views:

1390

answers:

3

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?

+4  A: 
Male: <%= Html.RadioButtonFor(x => x.Gender, "Male") %>
Female: <%= Html.RadioButtonFor(x => x.Gender, "Female") %>
Darin Dimitrov
That's pretty basic, but I'd argue that a proper use would include <label> tags with a "for" attribute matching it to a radio button.
Larsenal
+1  A: 

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

Mac
A: 

How to use labelFor in case of radio button since x.Gender doesn't have 2 different display names like male and female?

ninja