Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

asp.net core - Object reference not set to an instance of an object in Razor

I'm new to Razor and I'm getting this error when trying to loop through a list of objects.

enter image description here

This is my View:

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

This is the .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;
        }
    }
}

Here's the service

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

I tried to initialise the list in the comment but It did't work either :(

Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's better to see the implementation of quizService.GetQuestions() method. But you can check the variable after the method.

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

It's better to do this check in the GetQuestions() method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...