I have model like this :
public class Quotation
{
public System.Guid Id { get; set; }
public string Code { get; set; }
public DateTime Date { get; set; }
}
public class QuotationDetails
{
public System.Guid Id { get; set; }
public System.Guid QuotationId { get; set; }
public virtual Quotation Quotation { get; set; }
public virtual string quotationCode => Quotation.Code;
public virtual DateTime quotationDate => Quotation.Date; \\ <== This return error
public System.Guid MasterPriceUnitId { get; set; }
public virtual MasterPriceUnit MasterPriceUnit { get; set; }
public virtual string masterPriceUnitCode => MasterPriceUnit.Code;
}
And my controller for saving the edit is :
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult DetailsEdit([Bind(Include = "Id,QuotationId,MasterPriceUnitId")] QuotationDetails quotationDetails)
{
if (ModelState.IsValid)
{
db.Entry(quotationDetails).State = EntityState.Unchanged;
db.Entry(quotationDetails).Property("MasterPriceUnitId").IsModified = true;
db.SaveChanges();
return Json("success", JsonRequestBehavior.AllowGet);
}
return PartialView("_DetailsEdit", quotationDetails);
}
Weirdly it is accessing quotationDate
which is virtual field when saving and return error :
Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 83:
Line 84: public virtual DateTime quotationDate => Quotation.Date;Line 85:
I'm using many virtual field like that for one of my plugin. It is ok for many field, if I remove the quotationDate
field, it return no error and saved successfully.