0

When I update the the make of the vehicle on IndexMakeList page and delete the vehicle on IndexModel page I get the following errors respectively, though when I go back the pages vehicle has been updated or deleted

I get the NullReferenceException: Object reference not set to an instance of an object. AspNetCore.Views_Make_IndexMakeList.ExecuteAsync() in IndexMakeList.cshtml, line 30

An unhandled exception occurred while processing the request. NullReferenceException: Object reference not set to an instance of an object. AspNetCore.Views_Model_IndexModel.ExecuteAsync() in IndexModel.cshtml, line 39

I did the toggle breakpoints and what I understand is that after updating/deleting it goes back to the indexlistpage and loops through the list and tries to find the same id and where it gets the NullReferenceException. I know there are articles on this and I have read them but I am still failing to rectify this problem.I am not sure where the problem could be and which specific code to paste , so I am providing the github link for the project This is the github link for my project: https://github.com/life-traveler/Vroom/tree/master/Vroom. Thank you!!

StillLearning
  • 111
  • 4
  • 14
  • Please share a [mcve]. – mjwills Aug 26 '20 at 00:26
  • Short answer - `Model` is `null`. Likely because you forgot to pass the model as a parameter on the https://github.com/life-traveler/Vroom/blob/8fe4210003668853c936026addc7e69650e6e8fa/Vroom/Controllers/ModelController.cs#L43 line of code. – mjwills Aug 26 '20 at 00:27
  • Does this answer your question? [MVC 4 how pass data correctly from controller to view](https://stackoverflow.com/questions/18608452/mvc-4-how-pass-data-correctly-from-controller-to-view) – mjwills Aug 26 '20 at 00:28

1 Answers1

0

I tested your project, the problem is in the Delete method. The IndexModel View needs a IEnumerable<Vroom.Models.Model> type model, so you can't directly return the view. You can use RedirectToAction instead.

public IActionResult Delete(int id)
{
    Model modelUser = _vroomAppDbContext.Models.Find(id);

    _vroomAppDbContext.Remove(modelUser);
    _vroomAppDbContext.SaveChanges();
    return RedirectToAction("IndexModel");
}
mj1313
  • 7,930
  • 2
  • 12
  • 32
  • Thank you so much. I was so badly stuck here @mj1313. I didn't know this concept. It solved the problem. I learnt something which the tutorials I was watching didn't mention nor the articles that I read. Thank you!! – StillLearning Aug 27 '20 at 02:54