Updating multiple records in razor pages with a datatable

John Rowan 1 Reputation point
2022-02-14T18:07:41.197+00:00

Hi All,

I cant believe I have never faced this challenge before, but im a little bit stuck as what to do. I want to check a check box on individual rows and click one submit button to update each of the records at once but im having no joy. I am using a datatable inside a modal

Please see below a much shorter version of the datatable and then my c# code attempts.

I am showing a check box in the final column, that i want to use to update the isApproved field

var dataTable
$(document).ready(function () {
    dataTable = $('#approvalTable').DataTable({
        "select": true,
        "ajax": {
            "url": "?handler=event",
            "dataSrc": '',
            "type": "GET",
            "datatype": "jsonp",
            "responsive": "true",
            "fixedColumns": "true",
            "deferRender": true
        },
        "columnDefs": [
            {
                "targets": [0],
                "visible": false,
                "searchable": false
            },
            {
                "targets": [0, 1, 2],
                "className": "text-center",
                //, table - border tb
            }
        ],
        "columns": [
            { "data": "id" },
            { "data": "remarks" },
            { "data": "isApproved", },
            ,

            {
                "data": "id",
                "render": function (data) {
                    return `<div class="text-center">                     
                         <input class="form-check-input mt-3 mx-auto" type="checkbox" asp-for="Timesheeting.IsApproved">

                    </div>
                   `;
                },
            }

        ],
        "language": {
            "emptyTable": "No Staff Member Selected"
            ,
        },
    });


    [BindProperty]
            public Timesheets Timesheeting { get; set; }

    public async Task<IActionResult> OnPostEditRecords()
            {

                foreach (var timesheet in Timesheets)
                {
                    timesheet.IsApproved = Timesheeting.IsApproved;

                    _db.Entry(timesheet).State = EntityState.Modified;
                }
                await _db.SaveChangesAsync();
                return RedirectToPage("Index");
            }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,161 questions
{count} votes

1 answer

Sort by: Most helpful
  1. John Rowan 1 Reputation point
    2022-02-14T18:36:36.277+00:00

    I have tried several times to add more code, but im running into errors with the site

    0 comments No comments