Create an enum like so:
public enum Gender
{
Male = 1,
Female = 2
}
I'd alter your model slightly like so:
public Gender Sex { get; set; }
And then in your view you would do this:
Html.EditorFor(x => x.RegisterModel.Sex);
Then you would have an EditorTemplate here:
~/Views/Shared/EditorTemplates/Gender.cshtml
Which would have content like so:
@model EditorTemplate.Models.Gender // switch to your namespace
@Html.LabelFor(x => x, "Male")
@if(Model == EditorTemplate.Models.Gender.Male)
{
@Html.RadioButtonFor(x => x, (int)EditorTemplate.Models.Gender.Male, new { @checked = "checked" });
}
else
{
@Html.RadioButtonFor(x => x, (int)EditorTemplate.Models.Gender.Male);
}
@Html.LabelFor(x => x, "Female")
@if(Model == EditorTemplate.Models.Gender.Female)
{
@Html.RadioButtonFor(x => x, (int)EditorTemplate.Models.Gender.Female, new { @checked = "checked" });
}
else
{
@Html.RadioButtonFor(x => x, (int)EditorTemplate.Models.Gender.Female);
}
So I modeled this in Visual Studio, and this works as expected. Try it out.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…