1

So I post to the controller action below and I would like to "path" to this Car object's Client. When I attempt to do this, however, I get a

Null Reference Exception (Object reference not set to an instance of an object) error.


For some reason, Client is null.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ClientID,Name")] Car myCar)
{
    if (ModelState.IsValid)
    {
        db.myCars.Add(myCar);
        db.SaveChanges();

        //why is Client null here (Null Exception)?
        myCar.Client.UpdateLastModified();

        return RedirectToAction("Details", "Cars", new { id = myCar.ClientID });
    }
}

Is there any way to make this work? Also, if anyone could explain why this is happening, it would help me a great deal.

Thank you in advance.


edit: I'm using Linq-to-SQL, MVC 5, EF 6

mxmissile
  • 11,464
  • 3
  • 53
  • 79
M. Smith
  • 345
  • 3
  • 13
  • `myCar` is built from whatever you posted from your form. So check the data coming from the form post and see whether it has proper data for loading the `Client` property. – Shyju Aug 03 '16 at 13:46
  • Why doesn't EF/C# do this for me though? It seems like extra work to have to manually load Client before performing the needed action (i.e. UpdateLastModified()) I mean it's pretty obvious what I'm trying to do (load a related Client object)- I don't understand why EF and/or C# can't do this for me. – M. Smith Aug 03 '16 at 14:16
  • This is a create action where you are creating new entity from the data coming from form.rite ? If it is for an EDIT screen, using the unique Id, you need to query the db and get the entity. also if you are doing an edit, the best way to prevent overposting is to get the data from db and update whatever properties needed to be updated. http://stackoverflow.com/questions/34260334/mvc-6-bind-attribute-disappears/34260397#34260397 – Shyju Aug 03 '16 at 14:19
  • You are correct. Concerning overposting, using Bind (as I did in this example) should resolve the issue too. – M. Smith Aug 03 '16 at 14:24

1 Answers1

0

myCar.Client is not loaded, try re-loading the entity.

var car = db.mycars.First(x => x.Id == myCar.Id);
car.Client.UpdateLastModified();
mxmissile
  • 11,464
  • 3
  • 53
  • 79
  • I'm using Linq-to-SQL – M. Smith Aug 03 '16 at 13:43
  • Why does this Client object have to be re-loaded though? It seems like EF/C# should see what I'm trying to do and just reload it for me. Is there something I am missing here, or what? I don't understand why this doesn't just "work" as it is. – M. Smith Aug 03 '16 at 13:56
  • I'm not seeing where you are "loading" it. – mxmissile Aug 03 '16 at 14:21
  • You're correct, I'm not manually loading the Client object- I'm just wondering why EF/C# can't do this for me. It's obvious what I'm trying to do, right? This just seems liked extra (unneeded) work to me. Maybe I'm missing something, however, and just don't fully understand this. – M. Smith Aug 03 '16 at 14:22
  • I do see, and honestly I am not an EF expert. The only place it could happen in your code is in the Add() method. I just dont think it will hydrate/lazy load the entity at that point. Hopefully some EF experts will chime in. – mxmissile Aug 03 '16 at 14:36