I load multiple records from database table in jquery datatable and the process is fine run if i am not assign the ajax call back method i.e. success: function (data) {}.
But when i am assigned, not load the records.
Actually i want to that, if record is not found then new view(i.e. view name: create view) is open otherwise it is load the records into list view(i.e. view name: showGrid view)
Why is it???
Please help me any body......
What I have tried:
ShowGrid.cshtml file:
$(document).ready(function () {
$("#demoGrid").DataTable({
processing: true,
serverSide: true,
filter: true,
orderMulti: false,
paging: false,
scrollY: 200,
keys: true,
scroller: {
loadingIndicator: true
},
ajax: {
url: "/Demo/LoadData",
type: "POST",
datatype: "json",
success: function (data) {
if (data.recordsTotal == 0)
window.location.href = data.redirecturl;
}
},
columnDefs:
[{
targets: [0],
visible: false,
searchable: false
},
{
targets: [2],
orderable: false
},
{
targets: [3],
orderable: false
},
{
targets: [4],
orderable: false
},
{
targets: [5],
orderable: false
},
{
targets: [6],
orderable: false
},
{
targets: [7],
visible: false,
searchable: false,
orderable: false
},
],
columns: [
{ data: "CustomerID", name: "CustomerID", autoWidth: true },
{ data: "CompanyName", name: "CompanyName", autoWidth: true },
{ data: "ContactName", "title": "ContactName", name: "ContactName", autoWidth: true },
{ data: "ContactTitle", name: "ContactTitle", autoWidth: true },
{ data: "City", name: "City", autoWidth: true },
{ data: "PostalCode", name: "PostalCode", autoWidth: true },
{ data: "Country", name: "Country", autoWidth: true },
{ data: "Phone", name: "Phone", title: "Status", autoWidth: true },
]
});
});
--------------
Demo Controller file is:
public ActionResult ShowGrid()
{
return View();
}
public ActionResult Create()
{
return View();
}
public ActionResult LoadData()
{
try
{
using (DatabaseContext _context = new DatabaseContext())
{
var draw = Request.Form.GetValues("draw").FirstOrDefault();
var start = Request.Form.GetValues("start").FirstOrDefault();
var length = Request.Form.GetValues("length").FirstOrDefault();
var sortColumn = Request.Form.GetValues("columns[" + Request.Form.GetValues("order[0][column]").FirstOrDefault() + "][name]").FirstOrDefault();
var sortColumnDir = Request.Form.GetValues("order[0][dir]").FirstOrDefault();
var searchValue = Request.Form.GetValues("search[value]").FirstOrDefault();
int pageSize = (length != null && Convert.ToInt32(length) > 0) ? Convert.ToInt32(length) : 0;
int skip = start != null ? Convert.ToInt32(start) : 0;
int recordsTotal = 0;
var customerData = (from tempcustomer in _context.Customers
where 1==2 select tempcustomer);
if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDir)))
{
customerData = customerData.OrderBy(sortColumn + " " + sortColumnDir);
}
if (!string.IsNullOrEmpty(searchValue))
{
customerData = customerData.Where(m => m.CompanyName == searchValue);
}
recordsTotal = customerData.Count();
var data = customerData.Skip(skip).Take(recordsTotal).ToList();
return Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data });
}
}
catch (Exception ex)
{
throw ex;
}
}