Reputation: 2922
I'm creating a custom html helper that will allow me to group multiple checkboxes at once. I am using a span element in addition to the checkbox because I need to style the checkbox in a way that can't be done with the native element. The following is sample output of my custom HTML Helper:
<span class="checkbox" id="peopleInvolved-1"></span>
<input id="checkbox-peopleInvolved-1" name="peopleInvolved" style="display:none" type="checkbox" value="1"></input>
<label for="peopleInvolved-1">John Doe</label><br />
The user clicks on the span element (or optionally the label) and it mimics a checkbox through some jQuery magic. That part works great. The part I'm having trouble getting my head around is passing this data back to the controller properly. Currently I'm doing the following to retrieve the results:
public ActionResult Index(string[] peopleInvolved){}
I understand with my current setup that only checked values will return to the controller. I also understand this is the case due to an HTML limitation and that the ASP.NET native workaround is to create an additional hidden element alongside each checkbox. I have no problem adding an additional hidden input element but how would that apply in my scenario? I will have several checkboxes that are grouped together using the same name.
Thanks.
Upvotes: 0
Views: 2077
Reputation: 54628
Sounds like what you want to do is send back a list of string values from multiple html elements with the same name. What I would do is replace the checkbox with a hidden element, with an attribute like data-value="John Doe"
and populate or unpopulate the hidden element value on click of the html element of your choice.
Example available Here.
Upvotes: 1