Html (5) attributes for the editor

I know this has been discussed many times.

I would like to use

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

Unfortunatley is not possible by default, because EditorFor overwrites the Html attributes.

I do not want to use TextBoxFor, because I need the value to be formatted according to the DisplayFormat attribute.

Is there any solution for this?

+6
source share
2 answers

You can write your own editor template for a 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.

+10
source

Not sure if this is an option, but you can just put [Required] as the property decorator in the Model.

-1
source

All Articles