Question

Its my first time here, sorry if I wrote something wrong...

I have this HTML code, I need to get all the checkbox in a FormCollection

 [HttpPost]
      public ActionResult teste2(FormCollection collection)
            {
                var names = collection.AllKeys.Where(c => c.StartsWith("ckb") &&
                            collection.GetValue(c) != null &&
                            collection.GetValue(c).AttemptedValue == "1");

                return View();

            }

but I only get this result: allKeys string [0]. I tried do this using Ajax/Jason and other suggestions that I found on the web but I get the same result. I have no ideia what's wrong.
Please, help me! =]

@using (Html.BeginForm())
{ 
    <ul>
        @foreach (CWGD.BE.funcionalidade func0 in Model.func.Where(q => q.funcionalidade_pai == null))
        { 
            <li>
                <input type="checkbox" name="ckb_@(func0.funcionalidade_id)"/>
                @func0.funcionalidade_nome
                <ul>
                    @foreach (var func1 in Model.func.Where(q => q.funcionalidade_pai == func0.funcionalidade_id))
                    {

                        <li>
                            <input type="checkbox" name="ckb_@(func1.funcionalidade_id)"/>
                            @func1.funcionalidade_nome
                            <ul>
                                @foreach (var func2 in Model.func.Where(q => q.funcionalidade_pai == func1.funcionalidade_id))
                                {

                                    <li>
                                        <input type="checkbox" name="ckb_@(func2.funcionalidade_id)" id="ckb_@(func2.funcionalidade_id)" />
                                        @func2.funcionalidade_nome</li>

                                }
                            </ul>
                        </li>                
                    }
                </ul>
            </li>        
        }
    </ul>
    <input type="submit" value="Save" />
}
Was it helpful?

Solution

Your first job would be to launch Fiddler and see if the values are actually sent to the server. This is a generic advice that should be used every time any similar problem arises.

In your case the problem might be that your <checkbox> tags do not include value attribute so they will just send an empty value to the server.

Also you should provide the arguments to Html.BeginForm so that the form generates the correct target action.

Also remember that checkbox-es that are not checked, are never sent to the server in POST data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top