What Difference between TextBoxFor() and EditorFor() in MVC ?

Total Post:44


C#  HTML5  Mvc  Mvc3  Mvc4 
 447  View(s)
Rate this:

please explain it. 

  1. Re: What Difference between TextBoxFor() and EditorFor() in MVC ?

    TextBoxFor() vs EditorFor()

    What Difference between TextBoxFor() and EditorFor() in MVC ?

    These two are strongly-typed HTML helper methods and uses lambda expressions when referencing models passed to a view template.

    In MVC By default, the Create and Edit scaffolds now use the Html.EditorFor helper instead of the Html.TextBoxFor helper. It improves support for metadata on the model in the form of data annotation attributes when the Add View dialog box generates a view.

    TextBoxFor: Here, It will render like a text input HTML element compatible with specified expression. Into the simple word, it will always render like an input textbox irrespective datatype of the property which is getting bind with the control.

    EditorFor: Its controls are bit smart. Its render HTML markup based on the datatype of the property. Supposing there is a boolean property in the model. In rendering this property in the view as a checkbox either we can use CheckBoxFor or EditorFor. Both will generate the same markup.

    What is the advantage of using EditorFor?

    As we know, this creates HTML markup based on properties. Then suppose tomorrow if we change the datatype of property in the model, no need to change anything in the view. EditorFor control will change the HTML markup automatically.

    In TextBoxFor renders a text box for a particular field of the model in the view irrespective of the type of field. It considers the field as string type(By default).

    EditorFor renders different HTML form controls based on the type of field of the model. For example, if the field type is

    string - renders textbox,

    boolean - renders the checkbox

    datetime - renders input type="datetime" textbox

    int - renders intput type="number" textbox

    What Difference between TextBoxFor() and EditorFor() in MVC ?

      Modified On Jan-03-2019 12:58:54 AM


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.