Question

this is a conceptual question.

As far as I know, MVC has a stateless nature. When I hit reload (F5) in my browser, the values I wrote or changed in the view, doesn't go away nor get replaced by the original values from the model.

Does anyone know why is this happening? I'm assuming that in the moment I reload the page, the modified "not submitted" values should dissapear.

My views are only razor code... I'm not working with web forms (I mean, there are NO .aspx pages in my application)


@model PruebaMVC.Models.DatosLINQ.OPERACION

@{
    ViewBag.Title = "Edit";
}

<h2>Editar Operación</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Operación</legend>

        @Html.HiddenFor(model => model.IDOperacion)

        <div class="editor-label">
            @Html.LabelFor(model => model.FechaOperacion, "Fecha de la Operación")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.FechaOperacion)
            @Html.ValidationMessageFor(model => model.FechaOperacion)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Comision, "Comisión")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Comision)
            @Html.ValidationMessageFor(model => model.Comision)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Legajo, "Número de Legajo")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Legajo)
            @Html.ValidationMessageFor(model => model.Legajo)
        </div>

        <p>
            <input type="submit" class="formbutton" value="Guardar Cambios" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Volver al listado de Operaciones", "Index")
</div>

I hope not be inflicting any rule of the site by making a conceptual question. If I'm, please tell me, so I don't do this again.

Was it helpful?

Solution

ASP.NET MVC HTML helpers will use the values posted to recreate the form. If you want to ensure that your model's values get pushed to your view, remove them from the model state in your controller.

You can use either

public ActionResult MyController (MyModel model)
{
    if (ModelState.IsValid)
    {
        ModelState.Clear ();
    }

    return View (model);
}

or you can remove specific items you want to update

public ActionResult MyController (MyModel model)
{
    if (ModelState.IsValid)
    {
        ModelState.Remove ("PropertyNameToRemove");
    }

    return View (model);
}

I'm not sure where you're doing the updating (either manually in the controller, or in the model itself) but you just have to remove any or all of the items from ModelState and they will be repopulated.

See this question, as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top