0

Can someone please explain how to get values from all check boxes with the same name attribute from Request.Form object?

I have a View which has a List model. When I iterate through items, I display a Html.CheckBoxFor(modelItem => item.SomeBoolValue). Razor renders that value as a name of each checkbox element.

On the server-side, in a Controller, I try to extract the information with Request.Form.Get("item.SomeBoolValue"). It returns a series of trues and falses in a string. But when I try to access check boxes by index (Request.Form.Get("item.SomeBoolValue")[i]), it returns a weird code (102f or 97a, for example).

How should I properly get values from check boxes needed for my logic? Please help, I am going crazy about this. Searched everywhere, tried everything.

dzenesiz
  • 1,388
  • 4
  • 27
  • 58
  • 1
    You cannot use a `foreach` loop to generate form controls for collection items. Use a `for` loop or `EditorTemplate` (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943)) And do no use `Request.Form` - bind to your model! –  Sep 14 '16 at 10:21
  • I am not trying to generate form controls, I am trying to extract posted values on the server-side. Also, I am using `Request.Form` only because it won't bind no matter what I do. I put everything in a `@Html.BeginForm`, my `input` element is of type `submit`, so I couldn't figure that out and this looked more promising. – dzenesiz Sep 14 '16 at 10:29
  • 1
    You are generating form controls - what do you think `@Html.CheckBoxFor()` does. Read the dupe! –  Sep 14 '16 at 10:31
  • Oh, that... I am using a loop in a Controller action, too, so I misunderstood. Thank you very much, I will look into it. – dzenesiz Sep 14 '16 at 10:34

0 Answers0