1

I have created a table on my users page which shows all the restaurants they are following. But when I run the page I receive the error Object reference not set to an instance of an object. For the line

@foreach (var RestaurantSearch in Model.RestaurantSearch)

Is there a different way that this code should be written?

Profile Page code

@foreach (var RestaurantSearch in Model.RestaurantSearch)
        {
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
            var currentUser = manager.FindById(User.Identity.GetUserId());
            if (RestaurantSearch.User1ID == @currentUser.Id)
            { 
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => RestaurantSearch.RestaurantID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => RestaurantSearch.User1ID)
                </td>
                <td>

                    @Html.ActionLink("Details", "Details", "Restaurant", new { id = RestaurantSearch.RestaurantID }, null) |

                </td>
            </tr>
        }
        }

    </table>

Followviewmodel

public class Followviewmodel
    {
        public IEnumerable<BiteWebsite.Models.Followuser> UserSearch { get; set; }
        public IEnumerable<BiteWebsite.Models.FollowRestaurant> RestaurantSearch { get; set; }



        public string Name { get; set; }

        public string Cuisine { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string UserName { get; set; }
    }
Dolaps
  • 279
  • 2
  • 10
  • 25

1 Answers1

4

Either your Model or Model.RestaurantSearch is null. You have to set it in your Controller action.

Sample:

public ActionResult Index()
{
    Followviewmodel model = new Followviewmodel();

    // fill in the fields
    model.RestaurantSearch = ...

    return View(model);
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325