That is a tricky question because I want to add Html Attributes to the EditorFor() but do not want to replace the ones that was already created. Let me explain:

I have a default Editor Template for string.cshtml with the following code:

    var htmlAttributes = ViewData;
    htmlAttributes["class"] = "text-box single-line form-control";
    htmlAttributes["placeholder"] = ViewData.ModelMetadata.Watermark ?? ViewData.ModelMetadata.DisplayName;
    htmlAttributes["title"] = ViewData.ModelMetadata.Watermark ?? ViewData.ModelMetadata.DisplayName;
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, htmlAttributes)

It is used to always add the following class, placeholder and title, based on the DisplayName DataAnnotation, for the Form Inputs, it is quite handy!

But the problem is that I'm having trouble adding the disable attribute for one specific Form Input with the common code:

@Html.EditorFor(x => x.Field, new { htmlAttributes = new { disabled = "" } })

When the Form Input is with a field that is not a string, it will not follow the created EditorTemplate and it will work with this exactly code, but when it is a string, the EditorTemplate replaces the Html Attributes.

Does anyone has any clue on this?

Related posts

Recent Viewed