我如何用一个单选按钮传递多个值

本文关键字:单选按钮 何用一 | 更新日期: 2023-09-27 18:15:30

我正在创建一个web应用程序,我想要一个单一的选择(通过单选按钮)从用户传递值到我的表上的两个不同的列(一个字符串和一个int)。

我希望用一些简单的单选按钮语法技巧来解决这个问题,但是任何解决方案都是受欢迎的。

目前我的代码只是一个简单的razor HTML helper:

            <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)

我如何用一个单选按钮传递多个值

我创建了这两个扩展方法。您可以将第二个用于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