視圖模型:
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
好的,你想在視圖上用這個列表做什麼? –