Académique Documents
Professionnel Documents
Culture Documents
and is an expected feature in today's web applications. Meanwhile, the server validation logic is in
place because you should never trust information arriving from the network.
Required
3
STRINGLENGTH
Wikipedia says the longest name ever used belonged to a German
typesetter who lived in Philadelphia. His full name is more than 500
characters long.
4
REGULAREXPRESSION
you'd like to ensure the Email property of an Order contains a valid,
working e-mail address.
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}")]
[Range(35,44)]
The first parameter to the attribute is the minimum value, and the second
parameter is the maximum value. The values are inclusive.
The Remote attribute enables you to perform client-side validation with a server callback.
[Remote("CheckUserName", "Account")]
Inside the attribute you can set the name of the action, and the name of the controller the
client code should call.
Compare attribute.
By default, the ASP.NET MVC framework executes validation logic during model binding.
The MVC run time provides a validator to work with data annotations (the
DataAnnotationsModelValidator ).
This model validator can find all the validation attributes and execute the validation logic
inside. The model binder catches all the failed validation rules and places them into model
state.
Validation and Model State
Not only does model state contain all the values a user attempted to put
into model properties, but model state also contains all the errors
associated with each property (and any errors associated with the model
object itself).
Putting validation logic into a custom data annotation means you can easily
reuse the logic across multiple models. Of course, you have to write the
code inside the attribute to work with different types of models, but when
you do, you can place the new annotation anywhere.
On the other hand, adding validation logic directly to a model object often
means the validation logic itself is easier to write (you only need to worry
about the logic working with a single type of object). It is, however, more
difficult to reuse the logic.
CUSTOM ANNOTATIONS
IVALIDATABLEOBJECT
The method the MVC run time calls to perform
validation is named Validate instead of IsValid , but more
important, the return type and parameters are different.
[ScaffoldColumn(false)]
[ReadOnly(true)]
[DataType(DataType.Password)] [Display(Name="Password")]
The UIHint attribute gives the ASP.NET MVC run time the
name of a template to use when rendering output with the
templated helpers (like DisplayFor and EditorFor ).
HiddenInput