0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.