2014-02-28 174 views
1

我在我的用戶頁面上創建了一張表,其中顯示了他們所關注的所有餐館。但是,當我運行的頁面我收到錯誤Object reference not set to an instance of an object.對於線未將對象引用設置爲對象asp.net的實例c#

@foreach (var RestaurantSearch in Model.RestaurantSearch) 

是否有不同的方式,這個代碼應該寫的?

個人主頁代碼

@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; } 
    } 
+1

我敢打賭,你的控制器已經'返回查看()'而不是'返回查看(模型)' – Jonesopolis

+0

你能告訴我們的控制器,調用查看和發送模型參數? – Max

回答

4

無論您ModelModel.RestaurantSearch爲空。你必須在你的Controller動作中設置它。

樣品:

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

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

    return View(model); 
} 
相關問題