Non-static method requires a target
Solution 1:
I think this confusing exception occurs when you use a variable in a lambda which is a null-reference at run-time. In your case, I would check if your variable calculationViewModel is a null-reference.
Something like:
public ActionResult MNPurchase()
{
CalculationViewModel calculationViewModel = (CalculationViewModel)TempData["calculationViewModel"];
if (calculationViewModel != null)
{
decimal OP = landTitleUnitOfWork.Sales.Find()
.Where(x => x.Min >= calculationViewModel.SalesPrice)
.FirstOrDefault()
.OP;
decimal MP = landTitleUnitOfWork.Sales.Find()
.Where(x => x.Min >= calculationViewModel.MortgageAmount)
.FirstOrDefault()
.MP;
calculationViewModel.LoanAmount = (OP + 100) - MP;
calculationViewModel.LendersTitleInsurance = (calculationViewModel.LoanAmount + 850);
return View(calculationViewModel);
}
else
{
// Do something else...
}
}
Solution 2:
Normally it happens when the target is null. So better check the invoke target first then do the linq query.
Solution 3:
I've found this issue to be prevalent in Entity Framework when we instantiate an Entity manually rather than through DBContext which will resolve all the Navigation Properties. If there are Foreign Key references (Navigation Properties) between tables and you use those references in your lambda (e.g. ProductDetail.Products.ID) then that "Products" context remains null if you manually created the Entity.