0

when using @foreach in MVC 4 I get the error:Object reference not set to an instance of an object. I have called a list in outer loop and now am trying to call a list in inner loop.

@foreach (var item in Model.Users)
                        {
                           @:<tr>
                            <td>@item.Title</td>                                                                
                            <td>@item.Name</td>
                            <td>@item.Age</td> 

                             foreach (var user in Model.Company)
                             {
                                 <td>@user.CompanyName</td>
                             } 

                            @:</tr>                               
                        }
2
  • Believe it or not, there's more than one implementation of the MVC pattern in use in computing, in various different languages and technology stacks. I might guess that you're talking about ASP.Net MVC, with C# and Razor, but I shouldn't have to. Please edit your question and apply a few more targetted tags. Commented Feb 27, 2014 at 15:50
  • I am not 100% sure what the table is supposed to look like, but a few things. You don't need the @: near the opening and closing TR tags. Is it possible that a list of Companies was never mapped to the Model? You could try explicitly declaring it as a new list in the constructor of the Model. That would at least prevent it from blowing up. If it is always null, even when you know there is data, then im not sure what the issue would be. Commented Feb 27, 2014 at 16:25

1 Answer 1

1

Probably Model.Company is null. Check this.

How do you fill this list?

And you can remove the @: there are not needed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.