I want to show a radio button in my form which will be populated from model data.
here is my model
public class Student
{
[Required(ErrorMessage = "First Name Required")] // textboxes will show
[Display(Name = "First Name :")]
[StringLength(5, ErrorMessage = "First Name cannot be longer than 5 characters.")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last Name Required")] // textboxes will show
[Display(Name = "Last Name :")]
[StringLength(5, ErrorMessage = "Last Name cannot be longer than 5 characters.")]
public string LastName { get; set; }
[Required(ErrorMessage = "Sex Required")] // group of radio button will show
[Display(Name = "Sex :")]
public List<Sex> Sex { get; set; }
}
public class Sex
{
public string ID { get; set; }
public string Type { get; set; }
}
here I am trying to populate the student model manually from an action method like
public ActionResult Index()
{
var student= new Student
{
FirstName = "Rion",
LastName = "Gomes",
Sex= new List<Sex>
{
new Sex{ID="1" , Type = "Male"},
new Sex{ID="2" , Type = "Female"}
}
}
return View(student);
}
now how could I generate a radio button which will display text in form Male & Female
and as value will have the ID
I searched google and found many samples and I used one but not sure if it works. here is the radio button code in view.
@Html.RadioButtonFor(x => x.Sex, "Male")
I do not want to hard code male or female rather; I want to show it through a model and want to generate the radio button in a for loop.
I am new too MVC, so please guide me.