Saving DataTable Values
Saving DataTable Values
Ads123
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
This discussion has been closed.
Answers
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.
I managed to do both the tasks. I am putting the solutions here for others:
For DataTable refresh:
For Saving to Database:
Thanks
Hi,
Many thanks for posting back with your solution! I'm sure others will find it useful.
Allan