C# 编辑器模板集值的枚举RadioButton
基于这个问题,我实现了一个RadioButtonForEditor模板。我的工作很好,但目前你无法传递你想要选择的值C# 编辑器模板集值的枚举RadioButton,c#,asp.net-mvc,radio-button,asp.net-mvc-5,mvc-editor-templates,C#,Asp.net Mvc,Radio Button,Asp.net Mvc 5,Mvc Editor Templates,基于这个问题,我实现了一个RadioButtonForEditor模板。我的工作很好,但目前你无法传递你想要选择的值 EnumRadioButtonList.cshtml (Editor Template): @model Enum @foreach (var value in Enum.GetValues(Model.GetType())) { if ((int)value > 0) { @Html.RadioB
EnumRadioButtonList.cshtml (Editor Template):
@model Enum
@foreach (var value in Enum.GetValues(Model.GetType()))
{
if ((int)value > 0)
{
@Html.RadioButtonFor(m => m, (int)value)
@Html.Label(value.ToString())
}
}
我从视图中称之为:
@Html.EditorFor(m => m.QuestionResponse, "EnumRadioButtonList")
如何传递值QuestionResponse(enum)以便选中单选按钮?您可以在视图中这样更改
<input type="checkbox" id="checkbox1" name="checkbox" class="checkBoxId" value="@Model.checkbox"/>
然后提交表单您可以在视图中这样更改
<input type="checkbox" id="checkbox1" name="checkbox" class="checkBoxId" value="@Model.checkbox"/>
然后提交表单您可以在视图中这样更改
<input type="checkbox" id="checkbox1" name="checkbox" class="checkBoxId" value="@Model.checkbox"/>
然后提交表单您可以在视图中这样更改
<input type="checkbox" id="checkbox1" name="checkbox" class="checkBoxId" value="@Model.checkbox"/>
然后提交表单您可以创建一个自定义html帮助程序,该帮助程序将提供双向绑定
namespace YourAssembly.Html
{
public static class EnumHelpers
{
public static MvcHtmlString EnumRadioButtonListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression)
{
ModelMetadata metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
string name = ExpressionHelper.GetExpressionText(expression);
if (!metaData.ModelType.IsEnum)
{
throw new ArgumentException(string.Format("The property {0} is not an enum", name));
}
string[] names = Enum.GetNames(metaData.ModelType);
StringBuilder html = new StringBuilder();
foreach(string value in names)
{
string id = string.Format("{0}_{1}", name, value);
html.Append("<div>");
html.Append(helper.RadioButtonFor(expression, value, new { id = id }));
html.Append(helper.Label(id, value));
html.Append("</div>");
}
return MvcHtmlString.Create(html.ToString());
}
}
}
您可以创建一个自定义html帮助器,它将提供双向绑定
namespace YourAssembly.Html
{
public static class EnumHelpers
{
public static MvcHtmlString EnumRadioButtonListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression)
{
ModelMetadata metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
string name = ExpressionHelper.GetExpressionText(expression);
if (!metaData.ModelType.IsEnum)
{
throw new ArgumentException(string.Format("The property {0} is not an enum", name));
}
string[] names = Enum.GetNames(metaData.ModelType);
StringBuilder html = new StringBuilder();
foreach(string value in names)
{
string id = string.Format("{0}_{1}", name, value);
html.Append("<div>");
html.Append(helper.RadioButtonFor(expression, value, new { id = id }));
html.Append(helper.Label(id, value));
html.Append("</div>");
}
return MvcHtmlString.Create(html.ToString());
}
}
}
您可以创建一个自定义html帮助器,它将提供双向绑定
namespace YourAssembly.Html
{
public static class EnumHelpers
{
public static MvcHtmlString EnumRadioButtonListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression)
{
ModelMetadata metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
string name = ExpressionHelper.GetExpressionText(expression);
if (!metaData.ModelType.IsEnum)
{
throw new ArgumentException(string.Format("The property {0} is not an enum", name));
}
string[] names = Enum.GetNames(metaData.ModelType);
StringBuilder html = new StringBuilder();
foreach(string value in names)
{
string id = string.Format("{0}_{1}", name, value);
html.Append("<div>");
html.Append(helper.RadioButtonFor(expression, value, new { id = id }));
html.Append(helper.Label(id, value));
html.Append("</div>");
}
return MvcHtmlString.Create(html.ToString());
}
}
}
您可以创建一个自定义html帮助器,它将提供双向绑定
namespace YourAssembly.Html
{
public static class EnumHelpers
{
public static MvcHtmlString EnumRadioButtonListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression)
{
ModelMetadata metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
string name = ExpressionHelper.GetExpressionText(expression);
if (!metaData.ModelType.IsEnum)
{
throw new ArgumentException(string.Format("The property {0} is not an enum", name));
}
string[] names = Enum.GetNames(metaData.ModelType);
StringBuilder html = new StringBuilder();
foreach(string value in names)
{
string id = string.Format("{0}_{1}", name, value);
html.Append("<div>");
html.Append(helper.RadioButtonFor(expression, value, new { id = id }));
html.Append(helper.Label(id, value));
html.Append("</div>");
}
return MvcHtmlString.Create(html.ToString());
}
}
}
这对我不起作用,因为我不想管理ID。这是列表的一部分,因此不止一个。您可以创建一个列表,并在我测试它之前放入输入,它可以用于foreach(列表中的var项){}使用编辑器模板的好处之一是它可以管理ID,创建输入标记意味着我需要管理ID。这对我不起作用,因为我不想管理ID。这是列表的一部分,因此不止一个。您可以创建一个列表,并在我测试它之前放入输入,它可以用于foreach(列表中的var项){}使用编辑器模板的好处之一是它可以管理ID,创建输入标记意味着我需要管理ID。这对我不起作用,因为我不想管理ID。这是列表的一部分,因此不止一个。您可以创建一个列表,并在我测试它之前放入输入,它可以用于foreach(列表中的var项){}使用编辑器模板的好处之一是它可以管理ID,创建输入标记意味着我需要管理ID。这对我不起作用,因为我不想管理ID。这是列表的一部分,因此不止一个。您可以创建一个列表并在我测试它之前放入输入,它可以用于foreach(列表中的var项){}使用编辑器模板的好处之一是它管理ID,创建输入标记意味着我需要管理ID。