0

I am trying to load object Supplier from my database. Supplier has a foreign key Adress,so I use Include method to load Supplier,but I got an NULL REFERENCE EXCEPTION. This is my code in Controller:

public ActionResult LoadSupplier(Supplier supplier)
        {
             if (supplier.SupplierID==0)
             {
                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
             }
          Supplier sup= db.Supplier
                 .Include(d => d.Adress)
                 .FirstOrDefault(x=>x.SupplierID==supplier.SupplierID);

             if (sup== null)
             {
                 return HttpNotFound();
             }
     return View(sup);
        }

Also i try Explicit loading,but also got null reference:

  var sup=  db.Supplier.Single(x=>x.SupplierID==supplier.SupplierID);
                      db.Entry(sup).Reference(d => d.Adress).Load();

I appreciate any advice or help!

Nada Janjetovic
  • 113
  • 1
  • 15
  • Do you get the exception during the query or rendering of the view? – Mike_G Apr 26 '17 at 12:41
  • I think that is on rendering the view,because in other method when I am trying to update Supplier i put breakpoint and i saw that Supplier is not null,so i don't have Null reference. But in this method Supplier is null and I don't know why. When i pass supplier id on view and click on button i got Null reference on this line: Supplier sup= db.Supplier .Include(d => d.Adress) .FirstOrDefault(x=>x.SupplierID==supplier.SupplierID); – Nada Janjetovic Apr 26 '17 at 12:53

0 Answers0