我有男性和女性选项的单选按钮,但问题是它们都被选中而不是任何一个。
我的控制器:
private List<SelectedGenderModel> GetGender()
{
var gender = new List<SelectedGenderModel>();
gender.Add(new SelectedGenderModel { Name = "Male", Id = 1, Selected = false });
gender.Add(new SelectedGenderModel { Name = "Female", Id = 2, Selected = false });
return gender;
}
我的观点:
@model MvcDemo.Models.EmployeeModel
@for (int i = 0; i < Model.Gender.Count; i++)
{
@Html.RadioButtonFor(m => m.Gender[i].Selected, new { id = "gender_" + i, name = "gendernm_" })
@Html.HiddenFor(m => m.Gender[i].Id)
@Html.HiddenFor(m => m.Gender[i].Name)
@Html.DisplayFor(m => m.Gender[i].Name)
}
我的视图模型:
public class EmployeeModel
{
public int Id { get; set; }
public string Fullname { get; set; }
public List<SelectedGenderModel> Gender { get; set; }
}
我的数据模型:
public partial class EmployeeMaster
{
public int Id { get; set; }
public string Fullname { get; set; }
public Nullable<bool> Gender { get; set; }
}
那么如何防止多选单选按钮?