1

Let's say I've a two vanilla* html checkboxes in a Razor cshtml view.

  <input type="checkbox" name="tags[]" id="categorieOne" value="1">

  <input type="checkbox" name="tags[]" id="categorieTwo" value="2">

The first step would be to send this tags[] array to a controller.

The second step would be to get values 1 & 2 in seperated variables (example: in order to show "You've selected the following categories 1 ... 2" )

*By vanilla I mean they are not written with razor.

Ydhem
  • 928
  • 2
  • 14
  • 36

1 Answers1

5

If you rename your checkboxes from tags[] to tags, your controller action could take a string array as parameter which will hold the selected values:

<input type="checkbox" name="tags" id="categorieOne" value="1" />
<input type="checkbox" name="tags" id="categorieTwo" value="2" />

and then:

[HttpPost]
public ActionResult SomeAction(string[] tags)
{
    ... the tags array will contain the selected values (1, 2, ...)
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928