mercredi 27 avril 2016

c# Retrieving value in controller from dynamically created check boxes in view

I have the following code in my view:

@foreach (var x in model)
{
     <input type="checkbox" name="@x.name" /> @x.name
}

That loop will create about 10 check boxes with each of them having a unique name generated during the runtime. The whole point of this is to make this dynamic without me having to type the values of each name. So when I am trying to check in my controller if each of these check boxes are checked or not, how do I do it? Normally, I would pass a parameter to my controller

public ActionResult MyController(string/bool checkboxName)

and this would work fine if I had one checkbox. However, passing 10 parameters for each check box in the controller sounds insane! What if I add more to x in model later so that I have 20 check boxes?




Aucun commentaire:

Enregistrer un commentaire