CHECKBOX CONTROL IN MVC

In this article I am going to explain how to create checkbox control and how to get the selected checkboxes value in MVC.

Here in the Model class I have created a property named Hobbies that is of type List<string>, that return some natural hobbies such as Cricket, Football, Hockey stored in a property as I have mentioned below;

After completing the section of Model, here in the Controller class I have created two Action methods. First Index method that return a View along with the object of ModelClass and in the second action method i.e. Show which check the state of checkbox and stores selected checkbox value in a variable.

After performing the above tasks, you have to create the checkbox in a View which displays the hobby that is return by a property of Model. Also create a submit button on click of which selected hobbies are displayed or we can say selected checkboxes values are displayed.

Hi Kevin,I had checked your code and I got problem in your views. When you are creating check box in view, you are passing ID as "ckbox" which is incorrect according to logic of your controller. Please update your view code by code given below. Its work as I had check it.

If I don't use Html.BeginForm then how do I go about checking if a checkbox is checked in the Controller code since we don't have FormCollection anymore. What I mean is putting href of Url.Action next to the CheckBox in View but not sure how those checkboxes being captured in Controller.

Thanks Rohit. In my case, I really wish to avoid using Html.BeginForm in View since it screws up other controls in my page. Something like the following pattern that I currently have for those controls.

I should also mention that currently in my View page I have thumbnails and a checkbox for each. My goal is to: 1)process selected thumbnails 2)select thumbnails in current page 3) unselect thumbnails in page 4)select all 5)unselect all