What is ModelState.IsValid valid for in ASP.NET MVC in NerdDinner?
Solution 1:
ModelState.IsValid
tells you if any model errors have been added to ModelState
.
The default model binder will add some errors for basic type conversion issues (for example, passing a non-number for something which is an "int"). You can populate ModelState more fully based on whatever validation system you're using.
The sample DataAnnotations
model binder will fill model state with validation errors taken from the DataAnnotations
attributes on your model.
Solution 2:
From the Errata:
ModelState.AddRuleViolations(dinner.GetRuleViolations());
Should be:
ModelState.AddModelErrors(dinner.GetRuleViolations());
Reference: http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-MVC-1-0.productCd-0470384611,descCd-ERRATA.html