0

我正在创建一个 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)
4

1 回答 1

-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

没有什么花哨的,但有效。

于 2013-05-27T19:59:33.430 回答