0

I am looking for some feedback on custom EditorTemplates I have created. The purpose of my custom EditorTemplates is to make sure that the Bootstrap 3 CSS class form-control is always present, in addition to some appropriate HTML attributes depending on the data type of the view model's property.

However, I still want to be able to add additional CSS classes or various HTML attributes when actually using the EditorTemplate. For example, when I use

@Html.EditorFor(model => model.Something, new { htmlAttributes = new { @class = "datepicker" } })

I would expect to get HTML similar to this

<input type="text" class="datepicker form-control" id="Something" name="Something" />

I have my current code for my custom string EditorTemplate below (other data types are of course similarly done). This works, but I am wondering if I have made it too complicated? Any feedback would be appreciated.

@{
    var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(ViewData["htmlAttributes"]);

    if (!attributes.ContainsKey("placeholder") &&
        !string.IsNullOrWhiteSpace(ViewData.ModelMetadata.Watermark))
    { attributes.Add("placeholder", ViewData.ModelMetadata.Watermark); }

    if (!attributes.ContainsKey("aria-required") &&
        ViewContext.ViewData.ModelMetadata.IsRequired)
    { attributes.Add("aria-required", "true"); }

    // If the attributes dictionary already has a class attribute defined
    if (attributes.ContainsKey("class"))
    // Add the bootstrap class to it
    { attributes["class"] += " form-control"; }
    else
    // Create class attribute
    { attributes.Add("class", "form-control"); }
}
@Html.TextBox(string.Empty, ViewData.TemplateInfo.FormattedModelValue, attributes)
SvenAelterman
  • 1,532
  • 1
  • 15
  • 26
  • This should not go in a view. You should create you own `HtmlHelper` extension method and in the view use something like `BootstrapEditorFor()`. Refer [here](http://stackoverflow.com/questions/26162218/editortemplate-for-dropdownlist/26417466#26417466) and [here](http://stackoverflow.com/questions/30127866/create-checkboxfor-mvc-helper-with-title-attribute-from-model-description/30135407#30135407) and [here](http://stackoverflow.com/questions/34913037/refactor-similar-chtml-that-renders-varying-properties-using-editorfor-and-label/34913933#34913933) for some examples of what you can do. –  Jun 29 '16 at 22:30
  • @StephenMuecke Just to be clear, you're calling an EditorTemplate a view too? – SvenAelterman Jun 29 '16 at 22:53
  • 1
    An `EditorTemplate` is a partial view that is called by the built-in `EditorFor()` method. While you can do what your doing, if your going to repeatedly use this, creating an extension method means you can compile these into a separate dll and use them across multiple projects. –  Jun 29 '16 at 22:59
  • @StephenMuecke Thanks for the info. I don't think it will be beneficial to go as far as emitting my own HTML, so your [second reference](http://stackoverflow.com/questions/30127866/create-checkboxfor-mvc-helper-with-title-attribute-from-model-description/30135407#30135407) seems to be a good option. – SvenAelterman Jun 30 '16 at 13:48

0 Answers0