Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.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# 什么';在Asp.NETCore中实现是/否菜单的好方法是什么?_C#_Asp.net Core_Razor - Fatal编程技术网

C# 什么';在Asp.NETCore中实现是/否菜单的好方法是什么?

C# 什么';在Asp.NETCore中实现是/否菜单的好方法是什么?,c#,asp.net-core,razor,C#,Asp.net Core,Razor,我正在使用一些ASP.Net Core 3.1代码,其中一个表单有几十个Y/N菜单,如下所示: <div class="form-group" id="Q3_2_div"> <label>@Questions.Q3_2*</label> @Html.DropDownListFor(m => m.Answers.Q3_2, new SelectList(Enum.GetValues(typeof(Ye

我正在使用一些ASP.Net Core 3.1代码,其中一个表单有几十个Y/N菜单,如下所示:

<div class="form-group" id="Q3_2_div">
    <label>@Questions.Q3_2*</label>
    @Html.DropDownListFor(m => m.Answers.Q3_2, new SelectList(Enum.GetValues(typeof(YesNo))),
        "Select One",
         new { @class = "custom-select", @id = "Answers_Q3_2" })
</div>
<label>@Questions.Q140*</label>
@Html.DropDownListFor(m => m.Answers.Q140,
    new SelectList(Enum.GetValues(typeof(YesNo)).OfType<Enum>().Select(x => new SelectListItem
        { Text = Enum.GetName(typeof(YesNo), x), Value = Enum.GetName(typeof(YesNo), x) }), 
        "Value", "Text", YesNo.No),
    null, 
    new { @class = "custom-select", @id = "Answers_Q140" })  
<select asp-for="Answers.Q140">
     <option value="Yes">Yes</option>
     <option value="No">No</option>
</select> 
问:有没有一种更简单、更不冗长的语法可以替代?例如,你能帮忙吗

提前感谢您的任何建议

我会怎么做

我的版本可能如下所示:

<div class="form-group" id="Q3_2_div">
    <label>@Questions.Q3_2*</label>
    @Html.DropDownListFor(m => m.Answers.Q3_2, new SelectList(Enum.GetValues(typeof(YesNo))),
        "Select One",
         new { @class = "custom-select", @id = "Answers_Q3_2" })
</div>
<label>@Questions.Q140*</label>
@Html.DropDownListFor(m => m.Answers.Q140,
    new SelectList(Enum.GetValues(typeof(YesNo)).OfType<Enum>().Select(x => new SelectListItem
        { Text = Enum.GetName(typeof(YesNo), x), Value = Enum.GetName(typeof(YesNo), x) }), 
        "Value", "Text", YesNo.No),
    null, 
    new { @class = "custom-select", @id = "Answers_Q140" })  
<select asp-for="Answers.Q140">
     <option value="Yes">Yes</option>
     <option value="No">No</option>
</select> 

对
不

干净、简单、有效。

所以没有“自定义标记助手”:只有HTML“”上的“asp for”,对吗?问:这对我的“创建”表单来说是可行的。但是对于我的“编辑”表单,我如何将“Answers.Q140”的当前值设置为“default”选项?这是正确的,不需要自定义标记帮助器。“asp for”语法将使“Answers.Q140”的当前值成为“default”选项。我提供的代码将为您实现这一点。完美。我还确认:1)如果模型不可为空(例如,
public bool Q140{get;set;}
,那么标记帮助器将自动向“select”元素添加“required field”验证;2)对于我的“Create”表单,我可以将任何选项标记为“selected”,使其成为默认值;3)如您所说,标记助手将在我的“编辑”表单中自动将当前值标记为“选定”。好东西-谢谢!