0

When I look at an older MVC project, the following code would render a textbox with the propriate styling:

@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })

But now that JQueryUI is being used instead of Bootstrap, I had to manually add Bootstrap again and added the same line to my code, but the class won't render.

The only way to make it work it seems is using the:

@Html.TextBoxFor(model => model.License, new { @class = "form-control" })

HTML helper.

Is there a big difference between EditorFor and TextBoxFor and if it is important to use EditorFor instead of TextBoxFor, how could I add the Bootstrap class form-control to the rendered input by the EditorFor HTML helper? And what is causing this situation that the class won't be rendered on the input element with the HTML helper?

Barrosy
  • 1,407
  • 2
  • 25
  • 56
  • `@Html.EditorFor()` only supports adding html attributes if your using MVC-5.1 or higher (its not supported in MVC-4) –  Jun 09 '16 at 11:14
  • How come my Visual Studio has made such projects (possible for me) in the past and now makes these projects in which it is not possible then? – Barrosy Jun 09 '16 at 11:16
  • It would never have worked if your using MVC-4 (as tagged). And there is no difference if your just rendering `` The only advantage of `EditorFor()` is that will take into account `[DataType]` attributes and the property type that might generate say `type="date"` etc. –  Jun 09 '16 at 11:18

1 Answers1

1

TextBoxFor: Is always render like an input textbox irrespective datatype of the property which is getting bind with the control. It creates a text input like this : <input type="text" />

EditorFor: It renders HTML markup based on the datatype of the property.

Both will be generate the same markup.

You can also see explanations in this post: https://stackoverflow.com/a/4826185/3401842

Community
  • 1
  • 1
kkakkurt
  • 2,790
  • 1
  • 30
  • 33
  • I expected such answer. Then I guess it might be important to use EditorFor instead of TextBoxFor, is there a way to add the class I was refering to earlier, to an EditorFor rendered element? – Barrosy Jun 09 '16 at 11:25