4

In web forms the view state is used. But in ASP.NET MVC since model binding is available the properties can be easily acccessed in the controller. However when the model validation fails, does the ASP.NET MVC automatically populate the form controls realising that the validation has failed?

Or is there any other way by which this is accomplished.

1 Answer 1

10

There is an property called ModelState (in Controller class), that holds all the values. It is used in model binding. When validation fails, ModelState holds all the values with validation errors.

ModelState.IsValid tells you, that validation didn't throw any errors.

ModelState.Values holds all the values and errors.

EDIT

Example for Ufuk:

View model:

public class EmployeeVM
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Actions:

[HttpGet]
public ActionResult CreateEmployee()
{
    return View();
}

[HttpPost]
public ActionResult CreateEmployee(EmployeeVM model)
{
    model.FirstName = "AAAAAA";
    model.LastName = "BBBBBB";
    return View(model);
}

View:

@model MvcApplication1.Models.EmployeeVM

@using (Html.BeginForm("CreateEmployee", "Home")) {
    @Html.EditorFor(m => m)
    <input type="submit" value="Save"/>
}

As you can see, in POST method values are overwritten with AAAAA and BBBBB, but after POST, form still displays posted values. They are taken from ModelState.

11
  • Does that mean when the validation fails automatically the values from the modelstate.values are populated on to the form?
    – ckv
    Jun 15, 2013 at 11:50
  • @LukLed : Are u missing Required Fields in View Model ?
    – wuhcwdc
    Jun 15, 2013 at 13:29
  • @PankajGarg: This example was about ModelState, not required fields.
    – LukLed
    Jun 15, 2013 at 13:44
  • 2
    @PankajGarg - It's not necessary, it's simply best practice. Often your view has different validation requirements than your domain model, if that's the case then you can't pass your domain model directly (otherwise your validation will be incorrect). As an example, when you have a multi-step "wizard" where you are only filling in some fields of the model on any given step. Another reason is for security. Although there are ways to prevent unwanted binding, it's a hassle and easier to use a view model that only contains the data you need in the view. Jun 15, 2013 at 16:45
  • 2
    @PankajGarg - In general, View Models represent the concept of "least access", that is, you only pass the data that's required to the view, not an entire model that contains things you don't need. If you're only doing simple CRUD operations where everything matches up, and you have no sensitive information in the model, then sure.. go ahead, pass the domain model. However, most apps have more complex requirements. Jun 15, 2013 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.