我目前正在开展一个项目,在这个项目中,一个人可以登录并将工作保存为Darft。 我有一个扩展,将我的枚举转换为下面的单选按钮,这对于插入模式工作正常,但是当我从数据库中打开现有记录时,它没有预先填充保存的工作
public static MvcHtmlString RadioButtonForEnum<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression, object htmlAttributes, string nullLabel
)
{
var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
Type enumType = GetNonNullableModelType(htmlHelper.ViewData.ModelMetadata, metaData);
var names = Enum.GetValues(enumType).Cast<TProperty>();
var sb = new StringBuilder();
foreach (var name in names)
{
var description = GetEnumDescription(name).SplitCamelCase();
var id = string.Format(
"{0}_{1}_{2}",
htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix,
metaData.PropertyName,
name
);
var radio = htmlHelper.RadioButtonFor(expression, name, new { id = id }).ToHtmlString();
sb.AppendFormat(
"<label for=\"{0}\">{2}{1}</label> ",
id,
HttpUtility.HtmlEncode(description),
radio
);
}
return MvcHtmlString.Create(sb.ToString());
}
我的视图模型看起来像这样
public class TestModel
{
public int Id { get; set; }
[UIHint("EnumRadio")]
public Question1Enum? Question1 { get; set; }
[UIHint("EnumRadio")]
public Question2Enum? Question2 { get; set; }
[UIHint("EnumRadio")]
public Question3Enum? Question3 { get; set; }
}
我的枚举低于
public enum Question1Enum
{
[Description("20yrs")]
Option1,
[Description("5yrs")]
Option2
}
我的编辑模板位于
之下@model Object
@using (Html.BootstrapControlGroupFor(x => Model))
{
<br />
<div class="controls">
@Html.RadioButtonForEnum(x => Model)
@Html.ValidationMessageFor(model => Model)
</div>
}
请帮助!!!