Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have recently taken over support of an ASP.NET MVC project, and am trying to work through some of the errors, one in particular has me stumped though.

We have a 'New' page to add new items, with the following code running when the page is posted:

C#
[HttpPost]
public ActionResult New(RecordView i)
{
    if(ModelState.IsValid)
    {
        repository.AddRecord(i.DogIncident);

        return View("Index");
    }
    return View("Index");
}


However, when it tries to load the Index view, I get the following error: "Object reference not set to an instance of an object." and it points to the following block of code at the top of a file called RecordsView.cshtml:
@for (var i = 0; i < Model.Records.Count; i++)

The record does add correctly though, it just doesn't load the listings page correctly, and since this is just a "nice to have" I thought I'd simplify things by changing it so that it either returns some text which generates an error as it's expecting a boolean returned.

Any ideas on how to fix this please? I'm stumped.

Thanks!
Posted
Updated 28-Jul-15 0:52am
v2
Comments
jyo.net 28-Jul-15 7:03am    
can you share your index.cshtml code please

1 solution

Either "Model" is null, or "Model.Records" is null, use the debugger to find out why. I'm guessing it is Model though as you're not passing a model to your view.

return View("Index");


to pass a model you use the second param

return View("Index", ????);


We don't know what ???? is as we don't know your logic or your view code. It might be

return View("Index", i);


We don't know. This is all very basic MVC stuff though, I'd recommend you go through a book on MVC to at least understand the basics or else you're going to struggle.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900