This project is read-only.

RadioButtonList Model

The radio button list model can be used to correctly render out a list of radio buttons, including labels and valid html ids.
The RadioButtonList model is identical to the DropDown model, except that it uses the RadioButtonList template. It only contains two properties.

namespace MvcTemplates.Model
{
    public class RadioButtonList
    {
        public object Value { get; set; }
        public IEnumerable<SelectListItem> Options { get; set; }
    }
}

the marital status use case
public class EditMaritalStatusViewModel
{
    [Required]
    public RadioButtonList MaritalStatus { get; set; }
}

[HttpGet]
public ActionResult EditMaritalStatus()
{
    var maritalStatusRadioButtonList = new RadioButtonList()
                        {
                            Value = "Single",
                            Options = new SelectList( new[]{ "Single", "Married", "Divorced" } )
                        };
    var viewModel = new EditMaritalStatusViewModel
                        {
                            MaritalStatus = maritalStatusRadioButtonList
                        };
    return View( viewModel );
}

[HttpPost]
public ActionResult EditMaritalStatus( EditMaritalStatusViewModel model )
{
    var selectedValue = model.MaritalStatus.Value;
            
    //do something with the selected value
            
    return RedirectToAction( "EditMaritalStatus" );
}

You can customize the default wrapper that is rendered around each radio button through the Configuration object. (Templates.DefaultProfile.RadioButtonListRadioButtonContainer)
You can customize the classes that are rendered on each radio button by using Templates.DefaultProfile.RadioButtonListClasses.

Last edited Feb 22, 2011 at 4:06 AM by brk6004, version 2

Comments

No comments yet.