0

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.

Jun Rikson
  • 1,964
  • 1
  • 22
  • 43
  • I don't know why you mark it as duplicated since it is virtual field and nothing to do with saving either it is null or not null. The field not even called when savings. And it is fine with other field like Code, MasterPriceUnitCode, etc. Are you even reading this question? @Ehsan Sajjad – Jun Rikson Jan 05 '19 at 09:31
  • Pleas watch and check what is null,Quotation is null or Quotation.Date? if Quotation.Date is empty add Constructor to the Quotation object and set date to a value. – isaeid Jan 05 '19 at 09:33
  • Pleas watch and check what is null,Quotation is null or Quotation.Date? if Quotation.Date is empty add Constructor to the Quotation object and set date to a value. – isaeid Jan 05 '19 at 09:34
  • @isaeid It is not null, it is even a required field in actual code. And the one is saved is `QuotationDetails` not `Quotation`. Please read my controller code. – Jun Rikson Jan 05 '19 at 09:35
  • @JunRikson this is very common issue that developers face, debuggnig the code should help – Ehsan Sajjad Jan 05 '19 at 09:35
  • @EhsanSajjad It is not common. You are not even read the question and just close the question. When you saving a B object and one of virtual field mean NOT MAPPED field is referencing to another object said A. It will ignore it completely. In this case it is not ignored. Do you get it? – Jun Rikson Jan 05 '19 at 09:37
  • @JunRikson, I see, but your exception says "Object reference not set to an instance of an object." means an object which is required is null. – isaeid Jan 05 '19 at 09:41
  • you need to put hidden fields for the other object in view inside form – Ehsan Sajjad Jan 05 '19 at 09:41
  • @EhsanSajjad, did you read the end of my question. meaning my plugin require it. If not required I will create a viewmodel like my normal model. And your denying still did not answer my question. Even this is not best practice like you hoped. But this is crucial problem for me and it is not duplicated. – Jun Rikson Jan 05 '19 at 09:43
  • @isaeid : that is why I come here to stackoverflow and asking this question. Because this is not common for me. I am using many virtual field for my plugin, just like in this code. Im using 3 virtual code, and just this `Date` field is problem. If I remove it, this code will run and no errors. – Jun Rikson Jan 05 '19 at 09:45
  • @Backs : again? omg – Jun Rikson Jan 05 '19 at 10:16
  • Your `public virtual Quotation Quotation { get; set; }` in QuotationDetails needs to create in constructor, defining a property as virtual is not reason to creating instance of it automatically. In ef creating instance for such properties are automatic, but in this scenario we are in model binding point and the model is not managing with Ef. – isaeid Jan 05 '19 at 10:32
  • you can use code like this: `public virtual string quotationCode => Quotation == null ? (Quotation= new Quotation() {Id=this.QuotationId }) .Code: Quotation.Code; public virtual DateTime quotationDate => Quotation == null ? (Quotation = new Quotation() { Id = this.QuotationId }).Date : Quotation.Date;` – isaeid Jan 05 '19 at 10:35
  • @isaeid : Thank you for your helpings. I just stopped using the plugin and move to others for better practice models. I can't believed this question marked as duplicated twice. – Jun Rikson Jan 05 '19 at 10:42

0 Answers0