【发布时间】:2018-02-02 21:25:58
【问题描述】:
我想知道在 asp.net mvc 5 应用程序中显示组单选按钮的最佳方式是什么。
尝试 1:
@Html.RadioButton("SearchBy", "Name", true) <text> Name </text>
@Html.RadioButton("SearchBy", "Location") <text> Location</text>
[HttpPost]
public ActionResult Create(CoverLetterViewModel viewModel, string searchBy)
{//somecode}
尝试 2:
//prop
public string SelectedRoleType { get; set; }
//view
<label>
@Html.RadioButtonFor(m => m.SelectedRoleType, "JobSeeker", new { @class = "js-radio", id = "" })
<span>Job Seeker</span>
</label>
<label>
@Html.RadioButtonFor(m => m.SelectedRoleType, "Referrer", new { @class = "js-radio", id = "" })
<span>Referrer</span>
</label>
//controller action
[HttpPost]
public ActionResult Create(CoverLetterViewModel viewModel)
{//somecode}
尝试 3: 现在假设我有 10 个单选按钮,是否有任何有效的方法来创建单选按钮。我的意思是假设我有一个单选按钮列表,如下所示
public List<SOME RADIBUTTON ITEM> radiobuttonlist {get;set;}
问题:
我们能否以与处理 Dropdown Item 列表相同的方式显示单选按钮。我们能否以某种方式避免在视图文件中硬编码"Referrer" AND "JobSeeker" 文本。
【问题讨论】:
-
除非你创建了
HtmlHelper扩展方法,但你也可以使用循环——foreach (var item in Model.Roles) { @Html.RadioButtonFor(m => m.SelectedRole, item.Name) }
标签: c# asp.net-mvc razor checkbox asp.net-mvc-5