0
<div style="overflow-x:auto;">
<table id="customers">
    <caption><h3>Assign Privileges</h3></caption>
    <tr>
        <th>Modules</th>
        @foreach (var action in ViewBag.AllAction)
        {
            <th>@action.Action_name</th>
        }
        <th></th>
    </tr>
    @foreach (var data in ViewBag.allmodel)
    {
        <tr>
            <td>@data.Controller_Name</td>
            @foreach (var ac in ViewBag.AllAction)
            {
                <td>
                    <input type="checkbox" class="allchkbox" />
                </td>## `Heading` ##
            }
        </tr>
    }
</table>

This is my code.I have a checkbox in a loop thats why i cant find which checkbox is checked.In this view i want to know which row and column checkbox is checked.I want id each row and column that checkbox is checked.

This is my view.

  • What is it you want to know for each checked checkbox? - the row and column number? –  May 01 '18 at 05:55
  • Why don't you use the proper HTML structure here? Through distinct names or IDs, you should be able to solve the problem in seconds – Nico Haase May 01 '18 at 05:55
  • You want to get values of all checked checkboxes ? – Himanshu Upadhyay May 01 '18 at 05:56
  • yes i want to know each row and column number id. – Shubham Agarwal May 01 '18 at 05:59
  • Then you can use jquery to get the parent `` element, and the `.index()` method to get the column index (and ditto for the `` to get the row index) But it would easier to use nested `for` loops and add the row and column numbers as `data` attributes in your checkboxes –  May 01 '18 at 06:02
  • please send me code sample.i am too week in jquery. – Shubham Agarwal May 01 '18 at 06:09
  • Giving you some code to get the row and column indexers might help initially, but you have a x-y problem. Everything your doing in the view is fundamentally wrong (I assume because you do not know the correct approach). You need to bind your view to a model (not `ViewBag`) which means using nested `for` loops and you checkbox will bind to a `bool IsSelected` property. For a simple example, refer [this answer](https://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) –  May 01 '18 at 06:21
  • For an example using nested `for` loops, refer [this answer](https://stackoverflow.com/questions/29626914/how-to-represent-a-month-of-checkboxes-in-an-mvc-model/29627829#29627829) –  May 01 '18 at 06:22
  • thanx alot stephen – Shubham Agarwal May 01 '18 at 06:36

1 Answers1

0

you can simply add index id to every checkbox and the value of this index will be a number index to this checkbox enter image description here

Your Code will be like

@{int i = 0;}
@foreach (var data in ViewBag.allmodel)
{
    <tr>
        <td>@data.Controller_Name</td>
        @foreach (var ac in ViewBag.AllAction)
        {
            <td>
                <input id="@i" type="checkbox" class="allchkbox" />
            </td>## `Heading` ##
        }
        i++;
    </tr>
}

then you can select that input based on the id using jquery like this

var value_of_input_at_5 = $("#termsagreement:checked").val();
if( value_of_input_at_5 == true )
{
   alert("the value of button at index 5 is True" );
}else 
{
   alert("the value of button at index 5 is False" );
}
aa-Ahmed-aa
  • 363
  • 4
  • 14