Saving DataTable Values

Saving DataTable Values

Ads123Ads123 Posts: 5Questions: 2Answers: 1

Hi,

I have a datatable which allows multiple rows to be selected. It is populated in code behind:

public JsonResult AjaxHandler(DTParameters param)
        {
            try
            {
                var dtsource = new List<AdviserTarget>();
                using (ManagementEntities dc = new ManagementEntities())
                {
                    dtsource = dc.AdviserTargets.ToList();
                }

                List<String> columnSearch = new List<string>();

                foreach (var col in param.Columns)
                {
                    columnSearch.Add(col.Search.Value);
                }

                List<AdviserTarget> data = new ResultSet().GetResult(param.Search.Value, param.SortOrder, param.Start, param.Length, dtsource, columnSearch);
                int count = new ResultSet().Count(param.Search.Value, dtsource, columnSearch);
                DTResult<AdviserTarget> result = new DTResult<AdviserTarget>
                {
                    draw = param.Draw,
                    data = data,
                    recordsFiltered = count,
                    recordsTotal = count
                };
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new { error = ex.Message });
            }
         }

I have the following code in the cshtml file where I update a specific column for all selected rows:

// Handle form submission event 207719612
        $('#frm-example').on('submit', function (e) {
            var form = this;
            // Iterate over all selected checkboxes
            $.each(rows_selected, function (index, rowId) {
                // Create a hidden element 
                for (var i = 0; i < table.rows('.selected').data().length; i++) {

                    table.rows('.selected').data()[i].Comment = $("#comment").val();
                }
                $(form).append(
                    $('<input>')
                        .attr('type', 'hidden')
                        .attr('name', 'id[]')
                        .val(rowId)
                );
            });

My question is how to refresh the datatable so that the updates done above are visible and how to then save these updates back into the database (Entity Framework).

Thanks,

Ads

This question has an accepted answers - jump to answer

Answers

  • Ads123Ads123 Posts: 5Questions: 2Answers: 1

    For clarity:

    I am selecting multiple rows and then have a form on same page where the user can add a comment (it will be more then 1 field in the end product) and click submit. After this all the selected rows will have their comment column updated with the value from the form. This change will need to be saved to database (so bulk update?). I am not sure about the process of how to save all selected rows from datatable to database and refreshing datatable with updated value.

    I am not using Editor for this due to user requirements for the other fields so want to get this working with Comment field and column.

  • Ads123Ads123 Posts: 5Questions: 2Answers: 1
    Answer ✓

    I managed to do both the tasks. I am putting the solutions here for others:

    For DataTable refresh:

    table.ajax.reload(null, false); // user paging is not reset on reload
    

    For Saving to Database:

    // Handle form submission event 
            $('#form0').on('submit', function (e) {
                e.preventDefault();
                var form = this;
                // Iterate over all selected checkboxes
                $.each(rows_selected, function (index, rowId) {
                    // Create a hidden element 
                    for (var i = 0; i < table.rows('.selected').data().length; i++) {
                        table.rows('.selected').data()[i].Comment = $("#comment").val();
    
                        $.post("@Url.Content("/MI/SaveAT")",
                            {
                                'id': table.rows('.selected').data()[i].ID, 'comment': table.rows('.selected').data()[i].Comment
                            },
                            function () {
                                // success!
                                table.ajax.reload(null, false); // user paging is not reset on reload
                        }, "json");
                    };          
                });
            });
    

    Thanks

  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin

    Hi,

    Many thanks for posting back with your solution! I'm sure others will find it useful.

    Allan

This discussion has been closed.