Here is a quick example of using ViewBag. I would recommend switching and using the model for snapping. Here is a great article. model binding
Get method:
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(); }
Shipping method
[HttpPost] public ActionResult Index(FormCollection collection) {
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> }
source share