With my ASP.NET MVC, modal popup, DataTable and SSMS Server application, I have a problem when updating the dataTable.
When I click on edit button, it says Uncaught Type error: cannot read property of undefined.
HTML PAGE
| Category | Status | Edit | Delete |
|---|---|---|---|
$(document).ready(function () {
dataTable = $("#dataTable").DataTable({
"processing": false,
"serverSide": false,
"paging": true,
"destroy": true,
pageLength: 25,
dom: '<"html5buttons"B>lTfgitp',
"columns": [
{ "data": "CategoryName" },
{ "data": "Status" },
{
"data": "CategoryId", "width": "50px", "render": function (data) {
return 'Edit';
}
},
{
"data": "CategoryId", "width": "50px", "render": function (data) { return "Delete"; }
},
],
"ajax": {
"url": "/Home/GetAllCategory",
"type": "GET",
"dataSrc": "[]",
}
});
});
function getCategory($CategoryId) {
$("#categoryId").val($CategoryId);
if ($CategoryId != 0) {
jQuery.ajax({
url: '@Url.Action("UpdateCategory", "Home")',
data: ({
//insert your parameters to pass to controller
CategoryId: $CategoryId
}),
type: "Get",
dataType: "json",
contentType: 'application/json; charset=ut-8',
success: function (data) {
dataTable.ajax.reload();
console.log(data)
if (data != null) {
$("#categoryId").val(data.CategoryId);
$("#categoryName").val(data.CategoryName);
$("#status").val(data.Status);
}
}
});
} else {
$("#categoryId").val("");
$("#categoryName").val("");
$("#status").val("");
}
$("#categoryModal").modal('show');
}
CONTROLLER PAGE
public ActionResult GetAllCategory()
{
ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities();
var dataList = db.tblCategories.Where(x => x.Status == 1).ToList();
var data = dataList.Select(x => new
{
CategoryId = x.CategoryId,
CategoryName = x.CategoryName,
Status = x.Status
});
return Json(data, JsonRequestBehavior.AllowGet);
}
Vishal JoshiPosted Dec 6, 2022, 4:58 AM
Hello Emmmanuel,
Please review the Jquery code to call the controller action method. check with the below points.
Thank you
Emmmanuel FIADUFEPosted Dec 6, 2022, 9:38 AM
Thank you Vishal, it's working now