MVC Razor Radio Button

213,974

Solution 1

In order to do this for multiple items do something like:

foreach (var item in Model)
{
    @Html.RadioButtonFor(m => m.item, "Yes") @:Yes
    @Html.RadioButtonFor(m => m.item, "No") @:No
}

Solution 2

Simply :

   <label>@Html.RadioButton("ABC", True)Yes</label>
   <label>@Html.RadioButton("ABC", False)No</label>

But you should always use strongly typed model as suggested by cacho.

Solution 3

I done this in a way like:

  @Html.RadioButtonFor(model => model.Gender, "M", false)@Html.Label("Male")
  @Html.RadioButtonFor(model => model.Gender, "F", false)@Html.Label("Female")

Solution 4

I solve the same problem with this SO answer.

Basically it binds the radio button to a boolean property of a Strongly Typed Model.

@Html.RadioButton("blah", !Model.blah) Yes 
@Html.RadioButton("blah", Model.blah) No 

Hope it helps!

Solution 5

<label>@Html.RadioButton("ABC", "YES")Yes</label>
<label>@Html.RadioButton("ABC", "NO")No</label>
Share:
213,974
Nanu
Author by

Nanu

Updated on July 05, 2022

Comments

  • Nanu
    Nanu almost 2 years

    In partial view I work with textboxes like this.

    @model Dictionary<string, string>
    @Html.TextBox("XYZ", @Model["XYZ"])
    

    How can i generate radiobuttons, and get the desired value in the form collection as YES/NO True/False) ? Currently i am getting null for "ABC" if i select any value for the below.

       <label>@Html.RadioButton("ABC", @Model["ABC"])Yes</label>
       <label>@Html.RadioButton("ABC", @Model["ABC"])No</label>
    

    Controller

            public int Create(int Id, Dictionary<string, string> formValues)
            {
             //Something Something
            }
    
  • Nanu
    Nanu almost 12 years
    I call all my partial views passing @model Dictionary<string, string> i need a way to return (True, False) or (Yes,No) for selected values in radio buttons.
  • mattytommo
    mattytommo almost 12 years
    @KirkWoll his code wouldn't bind to a Dictionary<string, string> (as his model states). Based on a Yes No response, a bool is probably best, a Nullable bool if you're allowing none to be selected though.
  • mattytommo
    mattytommo almost 12 years
    @KirkWoll Check my updated edit, I originally thought he just wanted a Yes No for one option, thus recommending a bool.
  • Nanu
    Nanu almost 12 years
    I don't want to change my model, i want to keep @model Dictionary<string, string>
  • mattytommo
    mattytommo almost 12 years
    @Nikhil Ah well that's fine, you'll have either "Yes" or "No" as the values then (or null if nothing was selected). Try my updated edit and keep your model as Dictionary<string, string>
  • mattytommo
    mattytommo almost 12 years
    @Nikhil no problems glad I could help :)
  • Michael Brennt
    Michael Brennt over 10 years
    @:No / @:Yes is not working ( ':' is not allowed as a start of razor expression ) - maybe cause im not using loop
  • mattytommo
    mattytommo over 10 years
    @MichaelBrennt The "@:" is used to break out of Razor, similar to the <text> tag as Yes and No are basically HTML/non-C# code (you may not need it depending on your structure).
  • Maksim Vi.
    Maksim Vi. about 9 years
    It doesn't preserve field's state on postbacks if there's an error in the form.
  • Quality Catalyst
    Quality Catalyst over 6 years
    Explain your solution please.
  • Etienne Charland
    Etienne Charland almost 5 years
    You have to be careful as none of the posted solutions perform any kind of server-side validation. Here's an elegant solution that performs both client-side and server-side validation to ensure valid data is posted to the model. stackoverflow.com/a/56185910/3960200
  • InbetweenWeekends
    InbetweenWeekends almost 5 years
    This is the better answer because it addresses the rendering of unique id attributes.