3

I have a simple TestController class and User model:

public class TestController : Controller
{
    public ActionResult TestAction()
    {
        return View();
    }

    [HttpPost]
    public ActionResult TestAction(User user)
    {
        return View();
    }
}

public class User
{
    public int Id { get; set; }

    public string Name { get; set; }

    public int Age { get; set; }
}

This is my form:

enter image description here

As far as I know MVC is stateless and it does not have a viewstate concept. But after posting the data to my controller and when I return the view, all my data is there. I expect empty fields but they are all filled with the posted data. I could not understand how MVC knows the values after postback?

Omer
  • 8,194
  • 13
  • 74
  • 92
  • 3
    When you return a `View` from an `HttpPost` the assumption is that you are handling an error condition. It will keep the posted data in the `ModelState` and re-fill the data on the page so that the user can correct it. – stephen.vakil Oct 03 '16 at 19:16
  • And if you want an empty form after post then include a new User object when returning the View. – Stinky Towel Oct 03 '16 at 19:20
  • 1
    MVC passes the model back to the controller, if you pass the model back out to the view, all data is retained. On postback you determine if the model is valid, and perform the appropriate action(s). If you wish to return an empty view to the user, then you will have to cast a new model, or blank the fields before returning the data to the view. – Marc Lyon Oct 03 '16 at 19:22
  • @stephen.vakil, then can we say that, mvc has a different kind of viewstate? – Omer Oct 03 '16 at 19:30
  • 3
    @Omer K - "You must unlearn what you have learned". I'd let go of correlating this with ViewState. It's simple request and reply where your Controller is replying with the same values, this is different than persistence or statefulness. – Stinky Towel Oct 03 '16 at 19:34
  • 1
    Omer, I concur with Stinky. The data isn't persisting, nor is there a 'viewstate'. Data is being moved from a server to a view and back. You expect empty fields... As the programmer, you must empty them, or fill them to the needs of the project at hand. – Marc Lyon Oct 03 '16 at 21:09
  • When you submit the form, the values you have submitted are added to `ModelState`. When you return the view, the `HtmlHelper` methods first read the values from `ModelState` (which in your case exist) and sets the values of your controls based on those values (refer the 2nd part of [this answer](http://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) for an explanation of the behavior. –  Oct 04 '16 at 02:54
  • @stephen.vakil, please move your comment to answer. I will apply it. – Omer Oct 04 '16 at 07:46

2 Answers2

4

You Need to use ModelState.Clear()

public class TestController : Controller
{
    public ActionResult TestAction()
    {
        return View();
    }

    [HttpPost]
    public ActionResult TestAction(User user)
    {
        ModelState.Clear()
        return View();
    }
}
1

@stephen.vakil

When you return a View from an HttpPost the assumption is that you are handling an error condition. It will keep the posted data in the ModelState and re-fill the data on the page so that the user can correct it.

Omer
  • 8,194
  • 13
  • 74
  • 92