When I try to save an object, I get the error: Object reference not set to object instance
When I try to save an object, I get an error:
Object reference not set to object instance
I can create a new object, the code is almost the same.
When I try to save an object, I can trace a bit of code My controller code looks like this:
_entities.Kandidaats.Attach(kandidaatBewerken,true);
_entities.SubmitChanges();
return RedirectToAction("Index");
When I trace the code, I see that my kandidaatBewerken contains the correct data on the first line, after which the trace goes back to my view code and gives an error on the second line:
<% using (Html.BeginForm()) {%>
<%= Html.TextBox("KandidaatId", Model.KandidaatId)%>
When I trace it, it says that my model is empty, but just processed before, it is full.
Why am I getting this error and how can I move forward?
a source to share
I'm going to guess here .. because there isn't much code to go on ..
But the "Index" view creates a form to edit your Kandidaat object ...
However, you are redirecting your user to a form without a populated model (which is why the second line throws an error .. the model is null and you call the property on a null object).
try:
return RedirectToAction("Index", kandidaatBewerken);
But this will bring you back to shape (which may be your desired outcome). I would take the user back to any action that generates a list or some other route ...
return RedirectToAction("List");
or
return RedirectToRoute("Home");
a source to share