learning...
learning...

Reputation: 3184

MVC3 why use html.editorfor

I am looking at MVC3 razor examples and seeing html.editorfor being utilized and also asked about a lot on this forum. Why can't i use html.textboxfor and passwordfor? Thanks

Upvotes: 5

Views: 5160

Answers (2)

Philip Fourie
Philip Fourie

Reputation: 117047

EditorFor has the advantage that it will try to render an editor associated to the data type.

For example: If you design your own Editor Templates they will automatically be rendered based upon the property's type or UIHint

A useful editor template might be one that generates a date picker when the property's type is a DateTime.

There are some other scenarios as well where the 'smart' EditorFor will generate the 'best' editor for the property such an example is when it renders a <textarea> when tagging the property with MultilineText

Using TextBoxFor and PasswordFor are perfectly fine for those cases where you don't require 'a special editor'. It might even simplify your life when having to set HtmlAttributes.

Upvotes: 9

Niranjan Singh
Niranjan Singh

Reputation: 18260

Ref Differences between Html.TextboxFor and Html.EditorFor in MVC and Razor for clear the doubt about this..

The HtmlTextboxFor always creates a textbox (<input type="text" ...).

While the EditorFor looks at the type and meta information, and can render another control or a template you supply.

For example for DateTime properties you can create a template that uses the jQuery DatePicker.

if you decide to change something to the aspect of how your textboxes are rendered like wrapping them in a div you could simply write a custom editor template (~/Views/Shared/EditorTemplates/string.cshtml) and all your textboxes in your application will automatically benefit from this change whereas if you have hardcoded Html.TextBoxFor you will have to modify it everywhere.

Upvotes: 6

Related Questions