mehmetserif
mehmetserif

Reputation: 1227

Multiple checkboxes in mvc 3

I have multiple checkboxes with the same name. Like this:

    <input name="zones" value="@zoneItem.Id" type="checkbox" /><label>@zoneItem.Name</label></span>
<input name="zones" value="@zoneItem.Id" type="checkbox" /><label>@zoneItem.Name</label></span>
<input name="zones" value="@zoneItem.Id" type="checkbox" /><label>@zoneItem.Name</label></span>

but in get or post i can't get which ones are checked. How can i do it?

Upvotes: 1

Views: 2249

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

They should have different values. Right now you gave them all the same value. So once you give them different values:

<input name="zones" value="1" type="checkbox" />
<input name="zones" value="2" type="checkbox" />
<input name="zones" value="3" type="checkbox" />

in your controller action you will get the list of values of those that were checked:

public ActionResult Foo(string[] zones)
{
    ...
}

Upvotes: 3

JoJa
JoJa

Reputation: 610

They can be retrieved by adding a parameter to your action method.

public ActionResult GetData(Guid[] zones)
{
}

Make sure the array type matches your Id type.

Upvotes: 0

Related Questions