Tuesday, July 12, 2016

Difference between Html.EditorFor and Html.TextboxFor in ASP.NET MVC

If you are working on Web Application development using ASP.NET MVC, you would come across the Html.EditorFor and Html.TextboxFor in the Razor View.
The EditorFor is a kind of smart helper method. This will render the control based on the type in the model. For example, assume that you have a Boolean field in the model bind to the EditorFor. This will render checkbox.

Html.EditorFor

@Html.EditorFor(model => model.LastName)
This HTML helper method is very dynamic in nature. Based on which type of data is passed to this method, the output changes.

For example,
1. if model property type is of string type, it renders a textbox
2. if the property type is boolean type it renders a checkbox.
3. if the property type is of integer type, it render input type="number" textbox.

In order to control the output of the Html.EditorFor we can use EditorTemplates for different data types.

Html.TextBoxFor
@Html.TextBoxFor(model => model.LastName)
This HTML helper method only renders a TextBox as output irrespective of whatever data type the model property is of.

No comments :

Post a Comment