Blankman
Blankman

Reputation: 267280

Form with many checkboxes, how to get a list of id's?

I have a form, each row has a checkbox on it like:

<input type=checkbox id="cb-<%= o.ID %>" name="mycheckboxes" />

When the form is posted to an action, how can I get a list of the checked checkbox's id values?

Is it possible since they all have the same name?

Upvotes: 2

Views: 195

Answers (2)

Omar
Omar

Reputation: 40202

Store the value in the value attribute of the checkbox:

<input type="checkbox" value="<%= o.ID %>" name="mycheckboxes" />

In your action, tell ASP.NET MVC to look for an int[] with the name "mycheckboxes":

public ActionResult MyAction(int[] mycheckboxes)
{
   // do stuff here
}

Upvotes: 3

XtremeBytes
XtremeBytes

Reputation: 1497

I believe you are looking in javascript then Yes, use document.getElementsByName('') to get the list of all the checkbox and iterate thru them in the loop to find if its checked and get the ID.

Upvotes: 0

Related Questions