Pass values of checkBox to controller action in asp.net mvc4 Pass values of checkBox to controller action in asp.net mvc4 asp.net asp.net

Pass values of checkBox to controller action in asp.net mvc4


If a checkbox is checked, then the postback values will contain a key-value pair of the form [InputName]=[InputValue]

If a checkbox is not checked, then the posted form contains no reference to the checkbox at all.

Knowing this, the following will work:

In the markup code:

<input id="responsable" name="checkResp" value="true" type="checkbox" />

And your action method signature:

public ActionResult Index(string responsables, bool checkResp = false){    //Do Something}

This will work because when the checkbox is checked, the postback will contain checkResp=true, and if the checkbox is not checked the parameter will default to false.


For some reason Andrew method of creating the checkbox by hand didn't work for me using Mvc 5. Instead I used this

@Html.CheckBox("checkResp")

to create a checkbox that would play nice with the controller.


try using form collection

<input id="responsable" value="True" name="checkResp" type="checkbox" /> [HttpPost]public ActionResult Index(FormCollection collection){     if(!string.IsNullOrEmpty(collection["checkResp"])     {        string checkResp=collection["checkResp"];        bool checkRespB=Convert.ToBoolean(checkResp);     }}