Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RadioButtonFor in ASP.NET MVC 2

Can someone provide a simple example of how to properly use Html.RadioButtonFor? Let's say it's a simple scenario where my model has a string property named Gender. I want to display two radio buttons: "Male" and "Female".

What is the most clean way to implement this while retaining the selected value in an Edit view?

like image 388
Larsenal Avatar asked Apr 06 '10 22:04

Larsenal


People also ask

How do you add a radio button to a razor?

Razor Radio Buttons. Razor offers two ways to generate radio buttons. The recommended approach is to use the input tag helper. When creating a radio button, you must provide the value of one of the predefined options to the value attribute.


2 Answers

Male: <%= Html.RadioButtonFor(x => x.Gender, "Male") %>
Female: <%= Html.RadioButtonFor(x => x.Gender, "Female") %>
like image 64
Darin Dimitrov Avatar answered Oct 05 '22 06:10

Darin Dimitrov


This question on StackOverflow deals with RadioButtonListFor and the answer addresses your question too (@Larsenal it also includes labels with the "for" attribute)

like image 41
Mac Avatar answered Oct 05 '22 07:10

Mac