How to make model validation a list of elements in ASP.NET MVC?

Asked byjudith zvonkin

Tell me how you can validate the model (on the form) which is a list of items?


ASP.NET MVC 2 supports Data Annotations attributes. The attribute set is in

System.ComponentModel.DataAnnotations, it has been introduced since ASP.NET 3.5 SP 1. By default, the assembly

System.ComponentModel.DataAnnotations.dll is added when creating an ASP.NET MVC 2 project, if it doesn’t exist, its

must be added manually.

DataAnnotations validation attribute list:
RangeAttribute - indicates a numeric value constraint for a property;
RegularExpressionAttribute - indicates that the property must satisfy the specified regular expression;
StringLengthAttribute - indicates the maximum number of characters allowed in the property;
RequiredAttribute - indicates that the property is required;


public class Dinner {
 [Required (ErrorMessage = "Title is required")]
 public string Title {

 public ActionResult Create (Dinner dinner) {
 if (ModelState.IsValid) {
 // Dinner is valid, save it.
 else {
 return View ();
Here is a good article: ASP.NET MVC 2: Model Validation, in principle, this is one of the best MVC sites.
gil filar
I would recommend MvcExtensions for setting up validation. Introduction, in the section Metadata.
Where to buy trackpoint attachments? (TrackPoint Caps)? :: Exam preparation for MS Sharepoint 2010 :: Learn the style of the text on the site? :: How relevant is support for browsers without Javascript support now? :: Server analytics?
Leave Repply forHow to make model validation a list of elements in ASP.NET MVC?
Useful Links