Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HtmlHelper.EditorFor vs Html.TextBox vs Html input

I'm currently wetting my feet with ASP.NET MVC 3 w/ Razor, and I'm a bit confused about the most appropriate way to render HTML input elements.

As I see it, there are 3 methods to render those elements for a Model's property:

  1. Using the HtmlHelper EditorFor method
  2. Using Html.TextBox method (for string or numeric values)
  3. Using plain-old html input element, and using inline Razor tags to put the value in the element.

What is the recommended method for doing that? I'm a bit concerned with the EditorFor method, since I don't have any control over the rendered HTML, and I thought one of the mail goals of the MVC framework was to avoid the over-abstraction of WebForms.

What do you think? What is the best practice for this?

Thanks!

like image 377
ml123 Avatar asked Aug 25 '11 16:08

ml123


1 Answers

Html.EditorFor is the preferred way. You can control the HTML by writing custom editor templates for the given type. ModelMetadata allows you to control the type of the generated input field. If you need to apply additional custom attributes you could also write a custom metadata provider.

like image 164
Darin Dimitrov Avatar answered Oct 23 '22 08:10

Darin Dimitrov