0

ASP.NET MVC 4 application allows to mark table rows. Razor view contains:

@using (Html.BeginForm())
{
<table class="table table-hover">
    @foreach (var f in Model.Failid)
    {
        <tr>
            <td>@f.FailiNimi</td>
            <td>
                @Html.CheckBox("c" + f.Id.ToString())
        </td>
    </tr>
    }
</table>

<input type="submit" class="btn btn-success" />
        @Html.AntiForgeryToken()

        }

If submit button is pressed, browser sends post request with body like

c40=false&c9=true&c9=false&c9=false&c10=false&c13=false

Controller signature is

[HttpPost, ValidateAntiForgeryToken]
public ActionResult Failid(NameValueCollection nv)
{
...

However nv parameter value in controler is empty. Checkbox names are not passed as controller parameter.

How to get list of checked checkbox names in controller ?

Andrus
  • 26,339
  • 60
  • 204
  • 378
  • 1
    Have you considered Editor templates ? http://stackoverflow.com/questions/38961222/how-to-know-the-selected-checkboxes-from-within-the-httppost-create-action-metho/38964032#38964032 – Shyju Dec 10 '16 at 19:51
  • Do not use a `foreach` loop - refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943). And use a view model that represents what you want to edit - refer [this answer](http://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) –  Dec 10 '16 at 21:57

2 Answers2

1

Your MVC controller should take FormCollection:

HttpPost, ValidateAntiForgeryToken]
public ActionResult Failid(FormCollection nv)

If nothing still comes back, verify the name is being rendered with your expectation. Also, I don't remember completely but do you have to set your own value with checkbox? In order to post something, a value must be specified, and this value is what gets sent back to the server, and then converted to a boolean by MVC.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
0

Well, why would it show in a namevaluecollection? Try using Request.Form.AllKeys and Request.Form.GetValues(key)

TigOldBitties
  • 1,331
  • 9
  • 15