Data Annotations in Asp
Data Annotations in Asp
NET MVC
The MVC Framework provides several data annotations that you can
apply as attributes to a model.
Some of the important annotations that you can use in the models of
an ASP.NET MVC application are as follows:
Required
StringLength
RegularExpression
Range
Compare
DisplayName
ReadOnly
DataType
ScaffoldColumn
This annotation allows you to match a text string with a search pattern
that contains one or more character literals, operators, or constructs.
[RegularExpression(<pattern>)]
RANGE DATA ANNOTATION IN
MVC
You can use the Range annotation to specify the minimum and
maximum constraints for a numeric value.
where,
You can also explicitly state the text that the @Html.LabelFor() method
should display using the DisplayName annotation on the model
property.
READ ONLY DATA ANNOTATION IN
MVC
You can use the ReadOnly annotation to display read-only fields
on a form.
You can use this annotation to instruct the default model binder
not to set the specific property with a new value from the
request.
[ReadOnly(<boolean_value>)]
where,
[DataType(DataType.<value>)]
Password
MultilineText
Date
Time
SCAFFOLD COLUMN DATA
ANNOTATION
When you use scaffolding using the Create template, the view by default will
create UI fields for all the properties of the model.
However, you might need to ensure that the template does not create UI fields
for certain properties.
In such scenarios, you can use the ScaffoldColumn annotation passing a false
value.