class file Box.cs:
Using system;using system.collections.generic;using system.linq;using system.web;using system.componentmodel.dataannotations;using system.web.mvc;namespace mvccheckboxtest.models{Public class Box { [Display (Name = "hobby")] public ienumerable<selectlistitem> Hobbies {get; set;}} }
HomeController.cs file:
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;using Mvccheckboxtest.models;namespace mvccheckboxtest.controllers{public class Homecontroller:controller { //// GET:/home/public actionresult Index () { box model = new Box (); var roles = new[] { new SelectListItem {Value = "read", text= "reading", Selected = false}, new SelectListItem { Value = "Game", text= "Games", Selected = true}, new SelectListItem {Value = "Inter", text= "internet", Selected = false} }; Model. Hobbies = roles; return View (model); } Public ActionResult Submit (string[] Hobbies) { viewbag.hobbies = hobbies; return View ();}} }
Index.cshtml:
@model mvccheckboxtest.models.box@{ viewbag.title = "Index";}
Submit.cshtml:@{ viewbag.title = "Submit";}
AspNet MVC4 teaching -6:aspnet MVC4 CheckBox Control Test Demo