ASP.NET MVC > Models

Model field for a particular data type in ASP.NET MVC

How to validate a model field for a particular data type?


To validate a model property for a particular data type, use DataType attribute like below. 

[DataType(DataType.Currency)] 
public string Salary { get; set; }

The DataType enumerations has many values, some of them are 

Decorating the model field with DataType ensures that when it is rendered to the browser, it automatically appears in proper format (like currency format, email format etc.) provided the browser supports HTML 5.

[DataType(DataType.Date)]
public DateTime? BirthDate { get; set; }

Above property code would render "<input type="Date" name="BirthDate" />" that  would dispaly a calendar when user keeps the focus on the BirthDate text box as shown in below picture.

 Views: 20468 | Post Order: 37



Write for us






Hosting Recommendations