1

I have an ASP.NET 5 MVC application. I am using JQuery Datatables downloaded from Datatables.net for rendering my grid.

I want to be able to select multiple records on the grid and through the click of one button to be able to update the status feild on all my records. I can't figure out how to pass the information from javascript to my MVC controller through the view. Here is what I have so far.
View Code

    <table class="display " id="ExpenseTable">
    <thead>
        <tr>
            <th></th>
            <th>
                @Html.DisplayNameFor(model => model.ApplicationUserid)
            </th>

            <th>
                @Html.DisplayNameFor(model => model.ExpenseDate)
            </th>
            ...        </tr>
        </thead>
    <tbody>
        @foreach (var item in (IEnumerable<Expense>)ViewData["Expenses"])
        {
            <tr>
                <td></td>
                <td>                   
                   @Html.DisplayFor(modelItem => item.User.FullName)
                </td>

                <td>
                    @Html.DisplayFor(modelItem => item.ExpenseDate)
                </td>

               ....            
</tr>
    }
    </tbody>
</table>
@section Scripts {

<script>
    //script is not complete
    $(document).ready(function() {

        var table = $('#ExpenseTable').DataTable({
            dom: 'Bfrtip',
            buttons: [
                       {
                           text: 'Approve All Selected Expenses',
                           action: function () {

                           }
                       }
            ],
            columnDefs: [{
            orderable: false,
            className: 'select-checkbox',
            targets:   0
        } ],

    select: {
        style:    'multiple',
        selector: 'td:first-child'
    },

    order: [[1, 'asc']],

    } );
    } );

I want to do something like this in the Controller

  public IActionResult ApproveMany(IEnumerable<Expense> model)
        {
            foreach (var item in model)
           {
                item.Status = "Approved";
               _context.Update(item);
                _context.SaveChanges();
            }
            return RedirectToAction("StaffExpense"); ;
         }

What I need help with is how I can take the button I have and when it is pushed take a collection of all the expense items that the user has selected on the grid, and then pass that collection into my controller so the edited changes can be pushed back to the database.

2 Answers 2

1
$('#updateButton').click(function () 
{
    console.log("Selected rows: "+ table.rows('.selected').data().length);

    var data = $.map(table.rows('.selected').data(), function (item) 
    {
        console.log(item)
        return item;
    });
    //Call your MVC Controller/API to do the update (pass data)

    addData(data);

});
function addData(data)
{
  //Add your controller name to url
  /*POST*/
  $.ajax({
    url: '/Controller/ApproveMany',
    dataType: "json",
    type: "POST",
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify(data),
    cache: false,
    success: function (result) 
    {
        alert(result);
    },
    error: function (xhr) 
    {
        alert('error');
    }
  })
}
Sign up to request clarification or add additional context in comments.

1 Comment

Can you explain how to call my MVC controller called ExpenseController method- ApproveMany from the JavaScript code above.
0

This link would be helpful for you to get the desired solution

http://www.gyrocode.com/articles/jquery-datatables-checkboxes/

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.