Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
ASP.NET MVC 2
validation supports Data Annotations attributes. The attribute set is located in
System.ComponentModel.DataAnnotations, it has been present since ASP.NET 3.5 SP 1. By default, the
System.ComponentModel.DataAnnotations.dll assembly is added when creating an ASP.NET MVC 2 project, if it is not present, it
must be added manually.
List of validation attributes DataAnnotations:
RangeAttribute - indicates the limit of digital values for the property;
RegularExpressionAttribute - indicates that the property must match the given regular expression;
StringLengthAttribute - specifies the maximum number of characters allowed in the property;
RequiredAttribute - indicates that the property is required;
Example:
public class Dinner {
[Required(ErrorMessage = "Title is required")]
public string Title {
get;
set;
}
}
[HttpPost]
public ActionResult Create(Dinner dinner) {
if(ModelState.IsValid) {
// Dinner is valid, save it.
}
else {
returnView();
}
}
Here is a good article: ASP.NET MVC 2: Model Validation , in principle, this is one of the best sites for MVC.
To set up validation, I would recommend MvcExtensions. Introduction blog.hazzik.ru/post/19465323230/mvcextensions-intro to the Metadata section.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question