“未将对象引用设置为对象的实例。” Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>.Model.get 返回 null。?

问题描述 投票:0回答:1

调试器指向 foreach 部分,但我无法修复它。我不知道错误在哪里。希望这个问题能有一个正确的答案。

我的控制器:

namespace Seminar5.Controllers
{
    public class SeminarController : Controller
    {
        public IActionResult Apply()
        {
            return View();
        }
        [HttpPost]

        public IActionResult Apply(UserInfo model)
        {
            Repository.CreateUser(model);
            
            return View("Thanks",model);
        }

        public IActionResult List()
        {
            return View();
        }

        public IActionResult Details(int id)
        {
            return View(Repository.TakeId(id));
        }

我的模特:

public class UserInfo
{
    public int Id { get; set; }
    public string? Name { get; set; }
    public string? Phone { get; set; }
    public string? Email { get; set; }
    public bool WillAttend { get; set; }
}

我的存储库:

public class Repository
    {
        private static List<UserInfo> _users = new();

        public static List<UserInfo> Users
        {
            get
            {
                return _users;
            }
        }

        static Repository()
        {
            _users.Add(new KullaniciBilgi() { Id = 1, Name = "Busra", Email = "[email protected]", Phone = "111111111", WillAttend = true });
            _users.Add(new KullaniciBilgi() { Id = 2, Name = "Nur", Email = "[email protected]", Phone = "8384388438", WillAttend = true });
            _users.Add(new KullaniciBilgi() { Id = 3, Name = "Busra", Email = "[email protected]", Phone = "3438848384", WillAttend = true });
        }
       

        public static void CreateUser(UserInfo user)
        {
            _users.Add(user);
            user.Id = Users.Count + 1;
        }

       

        public static UserInfo? IdAl(int id)
        {
            return _users.FirstOrDefault(user => user.Id == id);
        }
    }
}

.cshtml 页面和 foreach 部分给出“对象引用...”错误:

@model List<UserInfo>

@{
    ViewBag.Title = "Status of Participating Into Seminar";
}
<main class="text-center">
    <h1 class="h4">Seminar Participation List</h1>
    <table class="table table-bordered">
        <thead>
            <tr>
                <td>Name</td>
                <td>Email</td>
                <td>Participance Status</td>
            </tr>
        </thead>
        <tbody>
            @{
                int i = 0;
            }
            @foreach(var user in Model) 
            {
                var status = user.WillAttend == true ? "Evet" : "Hayır";
                var classname = user.WillAttend == true ? "table-success" : "table-danger";
                if(user.WillAttend==true)
                {
                    i++;
                    <tr class="@classname"></tr>
                    <td>@user.Name</td>
                    <td>@status</td>
                    <td>
                        <a class="btn btn-sm btn-primary" href="/seminer/details/@user.Id">Detail</a>
                    </td>
                }
            }

我尝试运行它太多次,如果您愿意帮助我,我想在评论中与您分享如果项目上缺少细节或页面尚未共享。

c# model-view-controller foreach model
1个回答
0
投票

Object reference not set to an instance of an object.' Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>.Model.get returned null.? 
调试器指向 foreach 部分,但我无法修复它。我 不知道错误在哪里。我希望这个问题有一个真实的答案 问题。

根据您共享的代码片段和描述,错误的原因非常明显。您正在加载的视图需要一个

List<UserInfo>
列表,但尚未传递并且发生了空引用。

当我尝试重现您的问题时,我发现,在

Apply
操作方法中,您没有填充任何用户列表,因此出现以下错误:

如何修复:

为了修复您的异常,您应该将 userList 传递给该视图。你能做的是:

因此,从存储库中获取用户列表,因为您的存储库具有用户集合。你可以这样写

public IActionResult Apply()
 {

     var _listOfUsers = Repository.Users;
     return View(_listOfUsers);
 }

注意:我在您的存储库中发现了另一个问题,其中您对

_users.Add(new KullaniciBilgi()
进行静态存储库初始化,这是不正确的。根据您的
_users.Add(new UserInfo()
班级
 应该是 
UserInfo

输出:

© www.soinside.com 2019 - 2024. All rights reserved.