What determines if a field will be in the ViewData.ModelState Dictionary?
asp.net-mvc, c#, data-annotations, validation
Solution
The MSDN article for ModelStateDictionary says
Represents the state of an attempt to bind a posted form to an action method, which includes validation information.
So, the `ViewData.ModelState` dictionary contains values that are included in the request to the server. This is not necessarily all the properties on your bound model. If something you need is missing from this dictionary, include it as a hidden field in your view so it gets posted to the server.
Problem
Some background to my question: I am building a wizard style interface in ASP.Net MVC 4 that fills the properties of an object one page at a time. I use DataAnnotation attributes to specify my business rules and defined what a valid, filled-out object looks like. One complication that comes from this is validation. Since it takes several steps to completely fill out the object and make it valid, I have to validate each of the fields set by each step manually. This ends up looking like this in my controller class: ``` [HttpPost] public ActionResult Step1(MyBigModel m) { if (ViewData.ModelState["Field1"].Errors.Count() == 0 && ViewData.ModelState["Field2"].Errors.Count() == 0) { repository.saveStep1(m); return RedirectToAction("Step2", new { myId= m.myId}); } else return View(m); } } ``` The annoyance with this approach is that the fields I want to check are not always in the ModelState dictionary! If there is a validation error, the field will be in the dictionary. However, if it is valid, it may or may not be in the dictionary. It seems like some input types work better than others--textboxes seem to be fairly consistent, whereas checkboxes never show up. I haven't been able to figure out a consistent rule set for predicting when they'll be in there, nor have I found any relevant documentation. So, my question is, what determines if a field will be in the ViewData.ModelState Dictionary?