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>
Author by
Nanu
Updated on July 05, 2022Comments
-
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 almost 12 yearsI 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 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 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 almost 12 yearsI don't want to change my model, i want to keep @model Dictionary<string, string>
-
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 asDictionary<string, string>
-
mattytommo almost 12 years@Nikhil no problems glad I could help :)
-
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 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. about 9 yearsIt doesn't preserve field's state on postbacks if there's an error in the form.
-
Quality Catalyst over 6 yearsExplain your solution please.
-
Etienne Charland almost 5 yearsYou 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 almost 5 yearsThis is the better answer because it addresses the rendering of unique id attributes.