Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my code is

XML
<% using (Html.BeginForm())
       {%>
    <table>
        <tr>
            <th>
                LabelID_FK
            </th>
            <th>
                LabelName
            </th>
            <th>
                LabelIsDocument
            </th>
        </tr>
        <% foreach (var item in Model)
           { %>
        <tr>
            <td>
                <%: item.LabelID_FK %>
            </td>
            <td>
                <%: item.LabelName %>
            </td>
            <td>
              <%--  <input type="checkbox" value="df" id="chk" onclick="check()" />--%>
                <%=Html.CheckBox("chk_"+ item.LabelID_FK)%>
            </td>
        </tr>
        <% } %>
    </table>
    <p>
        <input type="button" value="submit" id="btn" />
    </p>





which show checkbox list for document label which user can select it . i want pass data list which user select checkbox it use jquery post what i do?
Posted

Here's[^] an example of a MVC3 checkboxlist as a Html Helper method.
 
Share this answer
 
Comments
javad_r_85 8-Jul-11 16:11pm    
no i want use jquery post
Jpuckett 8-Jul-11 16:14pm    
You can't use a jQuery post on every single click. A checkbox (when unchecked) sends NO VALUE to a html form when the form is submitted.

If you want your checkboxes to act as post triggers, then it's no longer a list unless you store a comma delimited list in a hidden field, use ViewBag, or something similar to store the values either in the client HTMl or server-side like the link I submitted.
I do not understand your comment about jQuery in previous solution comments.

If you want to receive an array (List<SomeClass>) in your control, you need to generate HTML similar to this:

XML
<input name="MyArray[0].Role" value="MyKey" type="hidden" />

<input id="MyKey" name = "MyArray[0].Selected" type="checkbox" checked = "checked" value="true" />

<input name="MyArray[0].Selected" value="false" type="hidden" />


Indexes must start at 0 (at least with MVC 2) and must be consecutive. Your SomeClass will typically then have a property MyKey that would identify the checkbox and another property Selected which will give the state.

In my case, since I was only interested in checked items, it seems that I do not set the key for unselected items.

I do not have source code I uses here at home... The HTML fragment was found by "View source code" in Internet Explorer.

I think there might be an easier way to do it by having a list in the model and properly render a partial view with nested names.... but I haven't tried it yet.
 
Share this answer
 
Comments
Philippe Mori 8-Jul-11 21:49pm    
In the controller, you can transform MyArray to something more suitable for processing like a List<MyKeyType> with a foreach loop or even some LINQ query.
There might be some cleaner way to do it but that method is relatively simple to implement.
i use this code when user click on button and work very good

[HttpPost]
        public ActionResult DocumentLabel(FormCollection model)
        {


            for (int i = 0; i < model.Count; i++)
            {
                AriaCRMEntities aria = new AriaCRMEntities();
                DocumentLabel label = new DocumentLabel();
                string lbl = model[i].ToString();
                string[] check = lbl.Split(',');

                bool chk = Convert.ToBoolean(check[0]);

                string name = model.Keys[i].ToString();
                string[] n = name.Split('_');
                string lblid = n[1];

                if (chk)
                {
                    label.LabelID_FK = Int32.Parse(lblid);
                    Guid id = Guid.NewGuid();
                    label.DocumentID_FK = id;

                    aria.DocumentLabels.AddObject(label);
                    aria.SaveChanges();
                }

            }


            return Content("0ok");
        }


but i want jquery post i need array check box whit select it to pass it to controller?
 
Share this answer
 
For jQuery, I do not understand well... Do you want that each time a check box is checked, a round trip is made to the server to post new values so that change are immediate?

If so, I think it would be best to have an action in your controller to just update the corresponding information on the server instead of posting all checkboxes.

If you still want to post the whole form then you might use something like
$(Your form selector here).submit();


and execute that code whenever a checkbox change. Assuming that you want to do it for all checkboxes on the web page, then it should not be hard to hook up the previous code to the appropriate event handler using jQuery.

The following site will help you with jQuery:

http://docs.jquery.com/Main_Page[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900