DataType DataAnnotation Attribute in Asp.net MVC3 Razor

  • The DataType attribute enables you to provide the run time with information about the specific purpose of a property. 
  • A property of type string can fill a variety of scenarios. It might hold an e-mail address , a URL, or a password. 
  • The DataType attribute covers all of these scenarios.

Example 1 :


In the above example, we are using DataType as password for password property.


UI :

For a DataType of Password, the HTML editor helpers in ASP.NET MVC will render an input element with a type attribute set to "password". In the browser, this means you will not see characters appear onscreen when typing a password.


Rendered HTML :

<input class="text-box single-line password" id="Password" name="Password" type="password" value="" />

As discussed earlier input type password is rendered on UI for DataType Password.


Example 2 :




In the above example, we have used MultilineText as DataType.  

UI :


For a DataType of Password, the HTML editor helpers in ASP.NET MVC will render Textarea element.

Rendered HTML :



<textarea class="text-box multi-line" id="address" name="address">
</textarea>

Thus the DataType.Multilinetext renders a Textarea element.

Other data types includes Currency, Date, Time, and ImageUrl.

0 comments:

Post a Comment