Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 编辑器模板集值的枚举RadioButton_C#_Asp.net Mvc_Radio Button_Asp.net Mvc 5_Mvc Editor Templates - Fatal编程技术网

C# 编辑器模板集值的枚举RadioButton

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

基于这个问题,我实现了一个RadioButtonForEditor模板。我的工作很好,但目前你无法传递你想要选择的值

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。