视图模型:
public class PersonViewModel
{
public int PersonId { get; set; }
public bool LikesIceCream { get; set; }
}
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new[]
{
new PersonViewModel { PersonId = 1, LikesIceCream = true },
new PersonViewModel { PersonId = 2, LikesIceCream = false },
new PersonViewModel { PersonId = 3, LikesIceCream = true },
};
return View(model);
}
[HttpPost]
public ActionResult Index(IEnumerable<PersonViewModel> model)
{
// you will get what you need here inside the model
return View(model);
}
}
视图(~/Views/Home/Index.cshtml
):
@model IEnumerable<PersonViewModel>
@using (Html.BeginForm())
{
@Html.EditorForModel()
<input type="submit" value="OK" />
}
编辑模板(~/Views/Home/EditorTemplates/PersonViewModel.cshtml
):
@model PersonViewModel
<div>
@Html.HiddenFor(x => x.PersonId)
@Html.RadioButtonFor(x => x.LikesIceCream, "true") Yes
@Html.RadioButtonFor(x => x.LikesIceCream, "false") No
</div>
为什么单选按钮?为什么不选择复选框?这是否意味着你只能有一个喜欢冰淇淋的人?似乎相当严格的应用程序:-) –
@Darin对不起,有人员记录列表。 – Mike
好的,你想在视图上用这个列表做什么? –