I am trying to implement the server side processing for the data tables plugin in C# ASP.NET MVC controller.
This is my code:
public async Task<ActionResult> AjaxHandler(jQueryDataTableParamModel param)
{
IEnumerable<Device> filteredItems;
if (!string.IsNullOrEmpty(param.sSearch))
{
filteredItems = _db.Devices
.Where(c => c.AccountCode.Contains(param.sSearch) ||
c.Company.ClientId.Contains(param.sSearch) ||
c.FirmVersion.Contains(param.sSearch));
}
else
{
filteredItems = _db.Devices.Include(x => x.User);
}
var displayedItems = filteredItems.Skip(param.iDisplayStart).Take(param.iDisplayLength);
var result = from c in displayedItems
select new[] {
c.AccountCode,
c.SerialNo ?? "None",
"Company Name",
c.User.FirstName};
return Json(new { sEcho = param.sEcho,
iTotalRecords = _db.Devices.Count(),
iTotalDisplayRecords = filteredItems.Count(),
aaData = result },
JsonRequestBehavior.AllowGet);
}
I am getting an error on the line where I select from displayedItems
.
I have debugged and found that the issue is specifically with c.User.Firstname
I have a feeling this is due to a lazy loading issue or something to do with the EF evaluates.
So my Question is why is EF giving me a null reference exception when I try and access the virtual Object User
I have even explicitly included it?
I am using EF 6.1
And yes I know the async is invalid here. I was hoping to do async database calls here. So if someone can point out to me where to add those I would also appreciate it.