簡體   English   中英

如何通過一個單選按鈕傳遞多個值?

[英]How do I pass multiple values with one radio button?

我正在創建一個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)

我做了這兩個擴展方法。 您可以將第二個與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

沒什么,但是有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM