I want to show Enum in EditorFor. I use Editor Template for show it.(DropDownList).
I have malty EditorFor in view. I want to set class for some controls.
@Html.EditorFor(m => m.Position, new { @class = "smallinput", style = "width:150px !important" })
@Html.EditorFor(m => m.DocumentType)
In Editor: Views/Shared/DisplayTemplates/Enum.cshtml
@model Enum
@{
var values = Enum.GetValues(ViewData.ModelMetadata.ModelType).Cast<object>()
.Select(v => new SelectListItem
{
Selected = v.Equals(Model),
Text = v.GetDisplayName(),
Value = v.ToString()
});
}
@Html.DropDownList("", values)
In Model
[DisplayName("نوع سند")]
[UIHint("Enum")]
public DocumentType DocumentType { get; set; }
@Html.EditorFor()with html attributes. For MVC 4, you will need to use@Html.TextBoxFor()or similar. Another alternative is pass the html attributes asAdditionalViewDataand use a customEditorTemplateAdditionalViewDatafor pass class to editor?EditorTemplateyou use to render the dropdown