I'm trying to work out if there's a built in way in ASP.NET MVC to assign the form values that are POST'd back to the properties of the ViewModel that was originally sent to the View?

So I'm thinking along the ideas of decorating some of the properties in the ViewModel with an attribute and then reflecting over the ViewModel and using that name to extract values (and coerce) from the Form[] object.

However, I'd imagine that something like this was already built in and so don't want to re-invent the wheel here.

The problem that I'm trying to solve is that a user clicks a button on a form and the server validates the data and if there are errors we return the user to the form by using the same ViewModel to carry the data and thereby fill the values back into the form that the user originally entered.

(Yes, I'm also doing client side validation using JavaScript to make this lightweight but for security I have to repeat validation on the server.)


Was it helpful?


You can use UpdateModel or TryUpdateModel in your controller.

I recommend using one of the overloads in which you specify the fields to be updated.

This is discussed in detail on page 78 of the Wrox Professional ebook (or echapter!)


I don't think MVC has anything like this built in, or at least I haven't seen anything. It would be nice though as many other MVC frameworks do this (struts for example).

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