As I iterate over my loop I am getting an when the iteration gets to OrderNo
System.NullReferenceException: Object reference not set to an instance of an object.
Code:
[HttpGet]
[Authorize]
public async Task<IActionResult> GetWorkSpaceNotifications()
{
WorkSpaceViewModel ReqWkspace = new WorkSpaceViewModel();
var reqWorkSpace = _context.Requisition.OrderByDescending(x => x.RequisitionId).ToList();
foreach (var c in reqWorkSpace)
{
ReqWkspace.departmentName = _context.Department
.Where(d => d.DepartmentId == c.DepartmentId)
.FirstOrDefault()
.DepartmentName;
if (c.RequisitionTypeId != null)
{
ReqWkspace.requisitionType = _context.RequisitionType
.Where(d => d.RequisitionTypeId == c.RequisitionTypeId)
.FirstOrDefault()
.Description;
}
ReqWkspace.ReqWorkSpace.RequisitionNo = c.OrderNo;
ReqWkspace.ReqWorkSpace.QueriesEmail = c.QueriesEmail;
ReqWkspace.ReqWorkSpace.DateCreated = c.DateCreated;
ReqWkspace.ReqWorkSpace.Total = c.Total;
ReqWkspace.Description = c.RequisitionTitle +""+ ReqWkspace.departmentName;
}
await Task.Run(() =>
{
});
return View(reqWorkSpace);
}