MVC Razor Radio Button

C#.Netasp.net Mvcasp.net Mvc-3Razor

C# Problem Overview


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
        }

C# Solutions


Solution 1 - C#

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 - C#

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 - C#

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 4 - C#

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 5 - C#

MVC5 Razor Views

Below example will also associate labels with radio buttons (radio button will be selected upon clicking on the relevant label)

// replace "Yes", "No" --> with, true, false if needed
@Html.RadioButtonFor(m => m.Compatible, "Yes", new { id = "compatible" })
@Html.Label("compatible", "Compatible")

@Html.RadioButtonFor(m => m.Compatible, "No", new { id = "notcompatible" })
@Html.Label("notcompatible", "Not Compatible")

Solution 6 - C#

<label>@Html.RadioButton("ABC", "YES")Yes</label>
<label>@Html.RadioButton("ABC", "NO")No</label>

Solution 7 - C#

MVC Razor provides one elegant Html Helper called RadioButton with two parameters (this is general, But we can overload it uptil five parameters) i.e. one with the group name and other being the value

<div class="col-md-10">
    Male:   @Html.RadioButton("Gender", "Male")
    Female: @Html.RadioButton("Gender", "Female")
</div>                         

Solution 8 - C#

<p>@Html.RadioButtonFor(x => x.type, "Item1")Item1</p>
<p>@Html.RadioButtonFor(x => x.type, "Item2")Item2</p>
<p>@Html.RadioButtonFor(x => x.type, "Item3")Item3</p>

Solution 9 - C#

This works for me.

@{ var dic = new Dictionary<string, string>() { { "checked", "" } }; }
@Html.RadioButtonFor(_ => _.BoolProperty, true, (@Model.BoolProperty)? dic: null) Yes
@Html.RadioButtonFor(_ => _.BoolProperty, false, (!@Model.HomeAddress.PreferredMail)? dic: null) No

Solution 10 - C#

I wanted to share one way to do the radio button (and entire HTML form) without using the @Html.RadioButtonFor helper, although I think @Html.RadioButtonFor is probably the better and newer way (for one thing, it's strongly typed, so is closely linked to theModelProperty). Nevertheless, here's an old-fashioned, different way you can do it:

    <form asp-action="myActionMethod" method="post">
        <h3>Do you like pizza?</h3>
        <div class="checkbox">
            <label>
                <input asp-for="likesPizza"/> Yes
            </label>
        </div>
    </form>

This code can go in a myView.cshtml file, and also uses classes to get the radio-button (checkbox) formatting.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNanuView Question on Stackoverflow
Solution 1 - C#mattytommoView Answer on Stackoverflow
Solution 2 - C#KuqdView Answer on Stackoverflow
Solution 3 - C#Cacho SantaView Answer on Stackoverflow
Solution 4 - C#Anjan KantView Answer on Stackoverflow
Solution 5 - C#Prasad De SilvaView Answer on Stackoverflow
Solution 6 - C#NanuView Answer on Stackoverflow
Solution 7 - C#Debendra DashView Answer on Stackoverflow
Solution 8 - C#BuhlebesizweView Answer on Stackoverflow
Solution 9 - C#jayson.centenoView Answer on Stackoverflow
Solution 10 - C#user5138047View Answer on Stackoverflow