2 回答

TA貢獻1865條經(jīng)驗 獲得超7個贊
您可能有一個視圖模型:
public class MyViewModel
{
public int Id { get; set; }
public bool IsChecked { get; set; }
}
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new[]
{
new MyViewModel { Id = 1, IsChecked = false },
new MyViewModel { Id = 2, IsChecked = true },
new MyViewModel { Id = 3, IsChecked = false },
};
return View(model);
}
[HttpPost]
public ActionResult Index(IEnumerable<MyViewModel> model)
{
// TODO: Handle the user selection here
...
}
}
視圖(~/Views/Home/Index.cshtml):
@model IEnumerable<AppName.Models.MyViewModel>
@{
ViewBag.Title = "Home Page";
}
@using (Html.BeginForm())
{
@Html.EditorForModel()
<input type="submit" value="OK" />
}
以及相應(yīng)的編輯器模板(~/Views/Home/EditorTemplates/MyViewModel.cshtml):
@model AppName.Models.MyViewModel
@Html.HiddenFor(x => x.Id)
@Html.CheckBoxFor(x => x.IsChecked)
現(xiàn)在,當(dāng)您提交表單時,您將獲得一個值列表,并且將為每個值選擇是否選中。
- 2 回答
- 0 關(guān)注
- 762 瀏覽
添加回答
舉報