Html(5) attributes for EditorFor

asp.net-mvc, asp.net-mvc-3, html, razor

Solution

You could write a custom editor template for the string type (`~/Views/Shared/EditorTemplates/string.cshtml`):

@Html.TextBox(
    "", 
    ViewData.TemplateInfo.FormattedModelValue,
    ViewData
)

and then:

@Html.EditorFor(u => u.Password, new { required = "required" })

will work as expected.

Problem

I know this has been discussed a lot of times. I'd like to use ``` @Html.EditorFor(u => u.Password, new { required = "required" }) ``` Unfortunatley this isn't possible by default as the EditorFor overwrites the Html attributes. I don't want to use TextBoxFor because I'd like the value to be formatted according to the DisplayFormat attribute. Is there any solution for this?

Original source