Question

i'm working with ASP.NET MVC at the moment and I found something with the viewModels that I do not understand. I woked out a very simple example, where this happens:

I have a simple view model with one Property:

public class Testmodel
{
    public int Test { get; set; }
    public Testmodel()
    {
        Test = 0;
    }
}

and I want to change the property in my controller like this:

    [HttpPost]
    public ActionResult Index(Models.ViewModels.Testmodel model)
    {
        model.Test += 1;
        return View(model);
    }

In my View, the effect of the change is only visible, when I access the property directly, not when i use Data Binding. The view looks like this:

@model hobble.Models.ViewModels.Testmodell
@{
    ViewBag.Title = "Startseite";
}
@using (Html.BeginForm()) {
<p>It is @Model.Test</p>   

<div>
    @Html.EditorFor(model => model.Test)
    </div>
    <input type="submit" value="Test" />
}

In the Text-Part, where I directly access the Model.Test property, I get the Update, but in the Editor field, I don't. I would expect that everytime I click the Submit-Button the Test Property is increased by one, but that is not Happening. Can someone explain to me, what I got wrong about that?

Was it helpful?

Solution

This is happening because whenever you return an object from a POST, in MVC, it is assuming that you are doing that because there was a validation error so instead of filling the form with the model values in your controller MVC is grabbing the values from the ModelState.

The quick "solution" is to use ModelState.Clear() but this is not recommend since you are basically ignoring MVC built-in functionality.

You could, as you already noticed, not use the Html Helpers or look into using the Post-Redirect-Get pattern.

Using ModelState.Clear(), just in case you want to ignore the "do-not-use-this-workaround" warning:

public ActionResult Index(Testmodel model)
{
    model.Test++;
    ModelState.Clear();
    return View(model);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top