1

I am trying to pass the value of all select checkbox to the controller. when i run the code I am having the error below and not all checkbox value are passed to the controller

"Uncaught TypeError: Cannot read property 'selected' of undefined"

Please can anyone tell me where I am going wrong

View Code

<table id="tblBulckTicket" class="table table-condensed table-striped table-hover datatable responsive" >
 <thead>
  <tr>
     <th>
     </th>
     <th>
        @Html.Label("id", "Ticket ID")
     </th>
     <th>
        @Html.Label("subject", "Subject")
     <th>
        @Html.Label("StageName", "Status")
     </th>
  </tr>
 </thead>
  <tbody>
  @foreach (var item in Model)
  {
  <tr>
     <td>
        <input type="checkbox" name="TicketId" id="TicketId" value="@item.Id">                                  
     </td>
     <td>
        <a class="pull-left btn btn-primary btn-xs" href="@Url.Action("Ticket", "Home" , new { id=item.Id})">
        <i class="fas fa-hashtag"></i>
        @Html.DisplayFor(modelItem => item.Id) </a>
     </td>
     <td>
        @Html.DisplayFor(modelItem => item.Subject)
     </td>
     <td>
        @Html.DisplayFor(modelItem => item.StageName)
     </td>
  </tr>
  }

Javascript code

    $(document).ready(function () {
        var table =   $('#tblBulckTicket').DataTable({
            "pageLength": 5
        });

        // Handle form submission event 
        $('#formId').on('submit', function (e) {
            var form = this;
            var rows_selected = table.column(0).checkboxes.selected();
            // Iterate over all selected checkboxes
            $.each(rows_selected, function (index, rowId) {
                // Create a hidden element 
                $(form).append(
                    $('<input>')
                        .attr('type', 'hidden')
                        .attr('name', 'TicketId[]')
                        .val(rowId)
                );
            });
              e.preventDefault();
        });  
       }); 
4

1 Answer 1

2

With help from Ourmandave Javascript code below

 $(document).ready(function () {
        var table =   $('#tblBulckTicket').DataTable({
            "pageLength": 5
        });
      $('#formId').on('submit', function (e) {
            var form = this;

            // Iterate over all checkboxes in the table
            table.$('input[type="checkbox"]').each(function () {
                // If checkbox doesn't exist in DOM
                if (!$.contains(document, this)) {
                    // If checkbox is checked
                    if (this.checked) {
                        // Create a hidden element 
                        $(form).append(
                            $('<input>')
                                .attr('type', 'hidden')
                                .attr('name', this.name)
                                .val(this.value)
                        );
                    }
                }
            });
        });
      });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.