Skip to main content
 首页 » 编程设计

c#之对象引用未设置为对象的实例 asp.net c#

2024年02月13日13dflying

<分区>

我在我的用户页面上创建了一个表格,其中显示了他们关注的所有餐厅。但是当我运行页面时,我收到错误 Object reference not set to an instance of an object. For the line

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

跟随 View 模型

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