Here comes a quick solution, let you have following properties in Model -
public bool PrintWeek1 { get; set; }
public bool PrintWeek2 { get; set; }
public bool PrintWeek3 { get; set; }
public string SelectedValue { get; set; }
Then your HTML should be like this -
@Html.RadioButtonFor(Model => Model.PrintWeek1, "PrintWeek1", new { @Name = "SelectedValue" })
@Html.RadioButtonFor(Model => Model.PrintWeek2, "PrintWeek2", new { @Name = "SelectedValue" })
@Html.RadioButtonFor(Model => Model.PrintWeek3, "PrintWeek3", new { @Name = "SelectedValue" })
Then when you submit the form, you will get the selected value in SelectedValue
property.
EDIT
To Address @StephenMuecke point, created the below solution -
Create a enum
-
public enum PrintWeekType
{
PrintWeek1, PrintWeek2, PrintWeek3
}
Then have a model property (instead of individual properties, have single emum property) -
public PrintWeekType SelectedValue { get; set; }
HTML should be like below -
@Html.RadioButtonFor(m => m.SelectedValue, PrintWeekType.PrintWeek1)
@Html.RadioButtonFor(m => m.SelectedValue, PrintWeekType.PrintWeek2)
@Html.RadioButtonFor(m => m.SelectedValue, PrintWeekType.PrintWeek3)
Using above sample, one can pre-select a radiobutton, at the same time we can post the selected value in SelectedValue
property.