Asp .Net MVC - SPLessons

ASP.Net MVC Data Annotations

Home > Lesson > Chapter 14
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

ASP.Net MVC Data Annotations

ASP.Net MVC Data Annotations

Data Annotation

shape Description

Data Annotation is used to add validation rules to our application by using simple attributes. Data Annotation contains different types of validation attributes to enforce the validation rules. To work with the data annotation, below namespace must be added.

Attributes of Data Annotation

Association

shape Description

Association is used to specify an entity member that represents the data relationship like Foreign key relationship.

Bindable

shape Description

Bindable attribute is used to specify whether the type is used to bind or not.

Compare

shape Description

Compare is used to provide an attribute that compares the two properties.

ConcurrencyCheck

shape Description

ConcurrencyCheck is used to specify whether a property will participate in optimistic concurrency check or not.

CreditCard

shape Description

CreditCard is used to check whether the field is credit card number or not.

CustomValidation

shape Description

CustomValidation is used to specify the user created method i.e used to implement custom validation to a property or class.

DataType

shape Description

DataType is used to specify the additional datatype name i.e required to be associated with the data field.

Display

shape Description

The display is used to provide a friendly name for the model property.

DisplayColumn

shape Description

DisplayColumn is used to specify the column name i.e displayed in the referred table as a foreign key format.

DisplayFormat

shape Description

DisplayFormat is used to specify how the data fields are to be display as well as the kind of format to be implemented.

Editable

shape Description

Editable is used to specify whether the data field is editable or not.

EmailAddress

shape Description

EmailAddress is used to validate the email id format.

FileExtensions

shape Description

FileExtensions is used to validate the Extension Name of the file.

Key

shape Description

Key is used to indicate the Unique Identifier.

MaxLength

shape Description

MaxLength is used to specify the Maximum length for the string.

MinLength

shape Description

MinLength is used to specify the Minimum length for the string.

Phone

shape Description

Phone is used to check whether the given number is in phone number format or not.

Range

shape Description

Range is used to specify some numeric range for data filed.

RegularExpression

shape Description

RegularExpression is used to specify a regular expression value for the data field.

Required

shape Description

Required is used to specify whether a data field value is required or not.

StringLength

shape Description

StringLength is used to specify minimum and maximum length for the string of a data field.

shape Example

Example Application on Data Annotations Follow the below steps to work with the Data Annotations.
  • Open a new MVC Application.
  • Create a Model as shown in the below figures
  • Then a new window will appear as shown in the below figure. Give the name as Employee.
  • Write the following code under the Employee class in the model.
[csharp] public class Employee { [Key] public int EmpId { get; set; } [Required(ErrorMessage="Please enter the EmpName")] public string EmpName { get; set; } [Required(ErrorMessage = "Please enter the EmpSalary")] public int EmpSalary { get; set; } [Required(ErrorMessage = "Please enter the EmpAddress")] public string EmpAddress { get; set; } [Required(ErrorMessage = "Please enter the EmpEmailId")] [EmailAddress(ErrorMessage="Please enter the valid email id")] public string EmpEmailId { get; set; } [Required(ErrorMessage = "Please enter the EmpPhone")] public string EmpPhone { get; set; } } [/csharp] Now add a view and write the following code. [html] @{ ViewBag.Title = "Employee"; } <h2>Employee</h2> @using (Html.BeginForm("Submit")) { <table> <tr> <td>@Html.Label("L1", "EmpId")</td> <td>@Html.TextBox("T1")</td> </tr> <tr> <td>@Html.Label("L2", "EmpName")</td> <td>@Html.TextBox("T2")</td> </tr> <tr> <td>@Html.Label("L3", "EmpSalary")</td> <td>@Html.TextBox("T3")</td> </tr> <tr> <td>@Html.Label("L4", "EmpAddress")</td> <td>@Html.TextBox("T4")</td> </tr> <tr> <td>@Html.Label("L5", "EmpEmail")</td> <td>@Html.TextBox("T5")</td> </tr> <tr> <td>@Html.Label("L6", "EmpPhone")</td> <td>@Html.TextBox("T6")</td> </tr> </table> <input type="submit" value="submit" /> } [/html] Now add a controller and give the name as Employee and write the following code. [csharp] public ActionResult Employee() { return View(); } [/csharp] Run the application. The following window will appear. Click on submit button. Then you will get error messages.