Michael Brandon Morris
Michael Brandon Morris

Reputation: 499

EditorFor with Enum named Color not using editor template

I have a property named Color of type Enum named Color, and I'm trying to use an editor template. My enum editor template is:

@ModelType System.Enum

<div class="form-group">
    @Html.LabelFor(Function(model) model)
    @Html.EnumDropDownListFor(Function(model) model, New With {.Class = "form-control"})
    @Html.ValidationMessageFor(Function(model) model, "", New With {.Class = "text-danger"})
</div>

This works for every property except Color which gets rendered as:

<input class="text-box single-line valid"
       data-val="true"
       data-val-required="The Color field is required."
       id="Color"
       name="Color"
       type="color"
       value="Black"
       aria-describedby="Color-error"
       aria-invalid="false">

@Html.EditorFor(Function(model) model.AnyOtherEnumProperty) works exactly as expected, but @Html.EditorFor(Function(model) model.Color) results in the HTML color input above. How can I get MVC to use the proper editor template?

Upvotes: 0

Views: 223

Answers (1)

Michael Brandon Morris
Michael Brandon Morris

Reputation: 499

To work around this issue, manually specify the editor template's name like so:

@Html.EditorFor(Function(model) model.Color, "Enum")

Upvotes: 0

Related Questions