How To Get CheckBox Selected Values in Controller In MVC3


In this article i am going to define u you how we can get selected values of checkbox in mvc3 in this article u will also get to know about multiple form in a single view how to use them.

Step 1: Make a new Empty Project using mvc3 in visual studio 2010.

Step 2: Now Add Collection Class in Model folder by Right click on model whose code would be such as below given.

 public class MyViewModel
        public int Id { get; set; }
        public bool Checkbox { get; set; }
        public string Value { get; set; }
    // view model for checklistbox
    public class CheckBoxListViewModel
        public List<CheckListBoxItem> Items { get; set; }
        // the name of this list should be the same as of the CheckBoxes otherwise you will not get any result after post
        public List<string> SelectedValues { get; set; }
    // represents single check box item
    public class CheckListBoxItem
        public string Value { get; set; }
        public string Text { get; set; }
        public bool IsCheck { get; set; }

Step 3: After this add a HomeController in Controller Folder and write in that below given code.

public ActionResult Index()
            var list = new List<MyViewModel>
                new MyViewModel { Id = 1, Checkbox = false ,Value="Coal"},
                new MyViewModel { Id = 2, Checkbox= false ,Value="Cotton"},
                new MyViewModel { Id = 3, Checkbox = false ,Value = "Fabric"},
            var outObj = list.Select(row => new SelectListItem()
                                Text = row.Value,
                                Value = row.Id.ToString(CultureInfo.InvariantCulture),
                                Selected = row.Checkbox
            ViewBag.keydata = outObj;
            // pass ViewModel object to View for display
            return View();

Step 4 :  Now Add your view index.cshtml in view folder and paste in his below given code.

    ViewBag.Title = "Index";
    var obj = ViewBag.keydata;
@using (Html.BeginForm("Index", "Home"))
    IEnumerable<SelectListItem> ct = ViewBag.keydata;
    foreach (var item in ct)
    @Html.CheckBox("chk", item.Selected, new { value = item.Value });<label>@item.Text</label>
       <input type="submit" value="Send" name="action:Send" /></div>

Step 5:  Now Make your Index Action with HttpPost Attribute in Controller which is below given.

        [MultipleButton(Name = "action", Argument = "Send")]
        public ActionResult Index(FormCollection obj)
            if (obj != null)
                string[] AllStrings = obj["chk"].Split(',');
                foreach (string item in AllStrings)
                    if (item.Contains("false"))
                        var outk = item;
                    // handle value
            return RedirectToAction("Index", "Home");
Now Run by pressing f5 and See by putting Result in Your action you will get your all selected
Check boxes values.

If you Want to Download complete source of this article you can get to download it by click on below given url


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s