How can I send the data to a post driver without the sent data being lost? MVC

2
@using (Html.BeginForm("Transactions", "Transactions", FormMethod.Post, new { @class = "formulario" }))
                {
                    @Html.AntiForgeryToken()
                    <div class="form-group">
                        <div class="input-group">
                            <div class="input-group-addon">
                                <i class="fa fa-calendar"></i>
                            </div>
                            <input type="text" id="rangofecha" name="rangofecha" class="form-control pull-right" />
                        </div><!-- /.input group -->
                        @Html.DropDownList("ConveniosList", (IEnumerable<SelectListItem>)ViewData["ConveniosList"], new { @class = "form-control pull-right", @id = "ConvenioId", @style = "color:black;" })
                        <div class="input-group">
                            <div class="input-group-addon">
                                <i>Ref.</i>
                            </div>
                            <input type="text" id="referencia" name="referencia" class="form-control pull-right" />
                        </div><!-- /.input group -->

                        @Html.ValidationMessage("rangofecha", "", new { @class = "text-danger" })
                    </div><!-- /.form group -->
                    <div class="row">
                        <div class="col-xs-12">
                            <button type="submit" class="btn btn-primary btn-block btn-flat"><i class="fa fa-search"></i>  Buscar</button>
                        </div><!-- /.col -->
                    </div>
                }

These data that are displayed on the screen I would like to keep them when the post returns the

  

ActionResult

    
asked by Elvin Acevedo 20.02.2017 в 20:54
source

1 answer

1

There are several options to do what you want, here I will indicate one using ViewData:

Your action that receives the POST should be something like the following:

[HttpPost]
public ActionResult Index(datetime fecha, string referencia)
{
    // Acá va tú código

    ViewData["fecha"] = fecha;
    ViewData["referencia"] = referencia;

    return View();
}

Now you must change the code in your view so that you can use these values, for example in the reference input:

<input type="text" id="referencia" name="referencia" class="form-control pull-right" value='@ViewData["referencia"]' />

You may also need to initialize the ViewData in your GET action:

[HttpGet]
public ActionResult Index()
{
    // Acá va tú código

    ViewData["fecha"] = DateTime.Now;
    ViewData["referencia"] = String.Empty;

    return View();
}
    
answered by 20.02.2017 / 21:14
source