【问题标题】:Object reference not set to an instance of an object in Razor对象引用未设置为 Razor 中的对象实例
【发布时间】:2021-04-11 19:40:57
【问题描述】:

我是 Razor 新手,在尝试遍历对象列表时遇到此错误。

这是我的观点:

@page
@model QuizModel
@{
    ViewData["Title"] = "Quiz Page";
}

<div class="text-center">
    <h1 class="display-4">Thanks for checking my first website out, @Model.Visitor.Name. Are you ready?</h1>
    <p>Let's see how much you know about me.</a>.</p>
</div>

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <form method="post">
        @foreach (var question in @Model.QuestionList)
        {
            <label>@question.Query</label>
        }

        <button type="submit">Send</button>
    </form>
</div>

这是.cs

namespace myquiz.Pages
{
    public class QuizModel : PageModel
    {
        [ViewData]
        [BindProperty]
        public string Name { get; set; }
        [BindProperty]
        public Visitor Visitor { get; set; }
        public List<Question> QuestionList { get; set; }

        public void OnGet()
        {
            var quizService = new QuizService();
            ///QuestionList = new List<Question>();
            QuestionList = quizService.GetQuestions();
        }

        public void OnPost()
        {
            Name = Visitor.Name;
        }
    }
}

这里是服务

 public class QuizService 
    {
        public List<Question> GetQuestions()
        {
            return new List<Question>()
            {
                new Question()
                {
                    Id=1,
                    Query = "What's my favourite band?",
                    Option1 = "Beatles",
                    Option2 = "Rolling Stones",
                    Option3 = "Led Zeppelin",
                    Answer = "Led Zeppelin"
                },
                new Question()
                {
                    Id=2,
                    Query = "What's my favourite colour?",
                    Option1 = "Pink",
                    Option2 = "Yellow",
                    Option3 = "Maroon",
                    Answer = "Pink"
                },
            };
        }
    }

我尝试初始化评论中的列表,但它也没有工作:(

谢谢!

【问题讨论】:

  • 我的回答有用吗?

标签: asp.net-core razor razor-pages


【解决方案1】:

最好看看 quizService.GetQuestions() 方法的实现。但是你可以在方法之后检查变量。

QuestionList = quizService.GetQuestions();
if (QuestionList is null)
    QuestionList = new List<Question>();

最好在 GetQuestions() 方法中进行此检查。

【讨论】:

    【解决方案2】:

    您需要初始化 Visitor 和 QuestionList,因为您在视图中使用它们。 你可以改变

    [BindProperty]
            public Visitor Visitor { get; set; }
            public List<Question> QuestionList { get; set; }
    

     [BindProperty]
            public Visitor Visitor { get; set; } = new Visitor();
     [BindProperty]
            public List<Question> QuestionList { get; set; } = new List<Question>();
    

    这样就不需要在 OnGet 中初始化了。

    更新:

    服务 DI: 启动:

    public void ConfigureServices(IServiceCollection services)
            {
                ...
                services.AddScoped<QuizService, QuizService>();
    
            }
    

    页面模型:

    public readonly QuizService quizService;
            public QuizModel(QuizService q){
                quizService = q;
            }
    
    public void OnGet()
            {
    
                QuestionList = quizService.GetQuestions();
            }
    

    【讨论】:

    • 当我这样做时,来自服务的数据没有通过。
    • 我已经更新了我的答案,在 QuestionList 上添加 [BindProperty]
    • 但是问题来自服务,所以我需要初始化它吗?否则它只是一个空列表。
    • 如果您可以确保每次在返回 Page 之前都会在处理程序中对其进行初始化,则不需要。像我在回答中显示的那样初始化它们可以帮助您避免出现 Object reference not set to an instance of an object 错误当您忘记在处理程序中初始化它时。
    • 这是有道理的。你能告诉我你将在哪里初始化“QuizService”实例吗?
    猜你喜欢
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多