MVC4:单个布尔模型属性的两个单选button

我试图find正确的Razor语法的互斥单选button,这些都反映了我的模型上的布尔属性的值。 我的模型有这个:

public bool IsFemale{ get; set; } 

我想用两个单选button来显示,一个是“男”,另一个是“女”,但是到目前为止,我所尝试过的所有东西都没有反映出模型上的IsFemale属性的实际值。 目前,我有这样的:

 @Html.RadioButtonFor(model => model.IsFemale, !Model.IsFemale) Male @Html.RadioButtonFor(model => model.IsFemale, Model.IsFemale) Female 

这似乎坚持正确的价值,如果我更改和更新,但不标记正确的值检查。 我相信这是愚蠢的,但我卡住了。

尝试像这样:

 @Html.RadioButtonFor(model => model.IsFemale, "false") Male @Html.RadioButtonFor(model => model.IsFemale, "true") Female 

这里是完整的代码:

模型:

 public class MyViewModel { public bool IsFemale { get; set; } } 

控制器:

 public class HomeController : Controller { public ActionResult Index() { return View(new MyViewModel { IsFemale = true }); } [HttpPost] public ActionResult Index(MyViewModel model) { return Content("IsFemale: " + model.IsFemale); } } 

视图:

 @model MyViewModel @using (Html.BeginForm()) { @Html.RadioButtonFor(model => model.IsFemale, "false", new { id = "male" }) @Html.Label("male", "Male") @Html.RadioButtonFor(model => model.IsFemale, "true", new { id = "female" }) @Html.Label("female", "Female") <button type="submit">OK</button> }