asp.net mvc radio button state

后端 未结 7 1872
轮回少年
轮回少年 2021-02-07 19:06

I\'m trying out asp.net mvc for a new project, and I ran across something odd. When I use the MVC UI helpers for textboxes, the values get persisted between calls. But, when I

7条回答
  •  温柔的废话
    2021-02-07 19:24

    I've made this HTML Helper 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
    
         _
        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("")
            For Each item In Items
                sb.AppendFormat("", Name, item.Value, If(item.Selected, "selected", ""), item.Text)
            Next
            sb.Append("
    ") Return sb.ToString() End Function

    Then in the view:

    <%= Html.RadioButtonList("ProviderType", Model.ProviderTypeSelectList) %>
    

    In the controller the option is mapped automagically using the standard:

    UpdateModel(Provider)
    

    Works like a charm. If you are tablephobic, change the markup generated.

提交回复
热议问题