我想在EditorFor中显示Enum
。我使用编辑器模板来显示它(DropDownList)。将类添加到MVC中的EditorFor
我有麦芽EditorFor
的意见。我想为某些控件设置类。
@Html.EditorFor(m => m.Position, new { @class = "smallinput", style = "width:150px !important" })
@Html.EditorFor(m => m.DocumentType)
在编辑:查看/共享/ 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)
在型号
[DisplayName("نوع سند")]
[UIHint("Enum")]
public DocumentType DocumentType { get; set; }
您需要MVC 5使用具有html属性的'@ Html.EditorFor()'。对于MVC 4,您需要使用'@ Html.TextBoxFor()'或类似的。另一种选择是传递HTML属性作为'AdditionalViewData'并使用自定义'EditorTemplate' – 2014-12-07 08:46:36
谢谢,我想在下拉菜单中显示枚举,所以我使用EditorFor.I使用MVC4。我可以使用'AdditionalViewData'作为传递类来编辑器? – 2014-12-07 08:56:22
你需要包括'EditorTemplate'你用来渲染下拉 – 2014-12-07 08:57:30