简体   繁体   中英

Object reference not set to an instance of an object when calling foreach loop

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>                               
                        }

Probably Model.Company is null. Check this.

How do you fill this list?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM