I have a Jquery datatable which is populated with Ajax sourced data. I need to show all the data from the Ajax source in the table checkbox checked to the controller. Since this is not strongly typed view I'm not sure what type of an object or array I have to pass to the controller method. The things that I have tried, always returns an empty result. Can anyone help me how to post table data to the controller? Thanks a lot in advance.
What I have tried:
<
My table:
<table id="jqueryTable" class="table table-striped table-bordered" cellspacing="0">
<thead>
<tr>
<th>
ID
</th>
<th>
PRIV_Name_Str
</th>
<th>
Create
</th>
<th>
Edit
</th>
<th>
View
</th>
</tr>
</thead>
function LoadProduct(element) {
$.ajax({
url: '/ADM_MAS_Privilege/GetFormData',
data: { YourValue: $('#productCategory').val() },
method: 'post',
dataType: 'json',
success: function (data) {
var table = $('#jqueryTable').dataTable({
paging: true,
sort: true,
searching: true,
scrollY: 200,
data: data,
bDestroy: true,
"columnDefs":
[{
"targets": 2,
"data": 'IsTaxExempt',
"render": function (data, type, full, meta) {
return data ? '<input type="checkbox" disabled checked/>' : '<input type="checkbox" enabled />'
}
},
{
"targets": 3,
"data": 'IsTaxExempt',
"render": function (data, type, full, meta) {
return data ? '<input type="checkbox" disabled checked/>' : '<input type="checkbox" enabled />'
}
},
{
"targets": 4,
"data": 'IsTaxExempt',
"render": function (data, type, full, meta) {
return data ? '<input type="checkbox" disabled checked/>' : '<input type="checkbox" enabled />'
}
}],
columns: [{ "data": "ID", "ID": "ID", "autoWidth": true },
{
"data": "PRIV_Name_Str", "PRIV_Name_Str": "PRIV_Name_Str", "autoWidth": true
},
{
"data": "Create", "Create": "Create", "autoWidth": true
},
{ "data": "Edit", "Edit": "Edit", "autoWidth": true },
{
"data": "View",
"orderable": false,
"type": "checkbox",
className: 'select-checkbox'
}
]
});
}
});
};
My save function:
$("form").on("submit", function () {
var data = table.$("input, select").serialize();
$.ajax({
type: "POST",
url: '/ADM_MAS_Privilege/save',
data: data,
success: function (response) {
}
});
This is my controller:
public JsonResult save(Array[] data)
{
bool status = false;
ADM_MAS_Privilege privladge = new ADM_MAS_Privilege();
privladge = datatoobject(data);
if (ModelState.IsValid)
{
db.ADM_MAS_Privilege.Add(privladge);
db.SaveChanges();
return new JsonResult { Data = new { status = status } };
}
return new JsonResult { Data = new { status = status } };
}