ASP.NET MVC Html.RadioButton Exception

后端 未结 5 1485
梦毁少年i
梦毁少年i 2021-02-19 01:23

I haver a simple radio button list on my page that I render with the following in my view:


<%=         


        
5条回答
  •  清酒与你
    2021-02-19 01:46

    This is very similar, if not equal to the checkbox issue: The Html.Checkbox() helper method generates a hidden field with a false value. If this field were missing, the browser would not send any data for uncheked boxes.

    A radiobutton, however, is supposed to have a value, and the possible values can be more than one. In this case it is not so easy to handle the non-selection case, which I guess is the reason it isn't.

    The workaround for me was to add a hidden field as follows:

    <%= Html.RadioButton("gender", 1) %> Male
    <%= Html.RadioButton("gender", 2) %> Female
    <%= Html.Hidden("gender", null) %>
    

提交回复
热议问题