ここでは、ViewBagを使用する簡単な例を示します。私はあなたのバインディングを行うためにモデルを切り替えて使用することをお勧めします。ここには素晴らしい記事があります。 Model Binding
入手方法:
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
List<string> items = new List<string>();
items.Add("Product1");
items.Add("Product2");
items.Add("Product3");
ViewBag.Items = items;
return View();
}
ポストメソッド
[HttpPost]
public ActionResult Index(FormCollection collection)
{
//only selected prodcuts will be in the collection
foreach (var product in collection)
{
}
return View();
}
HTML:
@using (Html.BeginForm("Index", "Home"))
{
foreach (var p in ViewBag.Items)
{
<label for="@p">@p</label>
<input type="checkbox" name="@p" />
}
<div>
<input id='btnSubmit' type="submit" value='submit' />
</div>
}