我正在创建一个Web应用程序,我希望用户的单个选择(通过单选按钮)将值传递到我表上的两个不同列(一个字符串和一个int)。
我希望用一些剃须刀单选按钮语法优雅的技巧解决这个问题,但欢迎任何解决方案。
目前我的代码只是一个简单的razor html帮助器:
<td>
<td>@Html.RadioButtonFor(model => model.MedicationPeriod, "Morning" ) </td>
<td>@Html.LabelFor(model => model.MedicationPeriod, "Morning")</td>
</td>
<td>
<td>@Html.RadioButtonFor(model => model.MedicationPeriod, "Morning and Night")</td>
<td>@Html.LabelFor(model => model.MedicationPeriod, "Morning and Night")</td>
</td>
我期望的是这样的事情(我知道不起作用):
@Html.RadioButtonFor(model => model.MedicationPeriod, "Morning", model => model.TimesPerDay, 1)
@Html.RadioButtonFor(model => model.MedicationPeriod, "Morning and Night", model => model.TimesPerDay, 2)
答案 0 :(得分:-1)
我已经制作了这两种扩展方法。您可以使用第二个列表SelectListItem
。
<Extension()> _
Public Function RadioButtonList(ByVal helper As HtmlHelper, ByVal name As String, ByVal Items As IEnumerable(Of String)) As String
Dim selectList = New SelectList(Items)
Return helper.RadioButtonList(name, selectList)
End Function
<Extension()> _
Public Function RadioButtonList(ByVal helper As HtmlHelper, ByVal Name As String, ByVal Items As IEnumerable(Of SelectListItem)) As String
Dim sb As New StringBuilder
sb.Append("<ul class=""radiobuttonlist"">")
For Each item In Items
sb.AppendFormat("<li><input id=""{0}_{1}"" name=""{0}"" type=""radio"" value=""{1}"" {2} /><label for=""{0}_{1}"" id=""{0}_{1}_Label"">{3}</label></li>", Name, item.Value, If(item.Selected, "selected", ""), item.Text)
Next
sb.Append("</ul>")
Return sb.ToString()
End Function
没什么特别的,但是很有效。