NullReferenceException:对象引用未设置为对象ASP.NET Core

时间:2019-12-23 10:21:32

标签: c# asp.net asp.net-core

我正在使用ASP.NET Core,我有两个类Poll.cs和PollOption.cs

[Table("Polls")]
public class Poll
{
    [Key]
    public int PollID { get; set; }
    [DataType(DataType.Date)]
    public Nullable<DateTime> AddedDate { get; set; }
    public string AddedBy { get; set; }
    public string Path { get; set; }
    public string QuestionText { get; set; }
    public bool IsCurrent { get; set; }
    public bool IsArchived { get; set; }
    public Nullable<DateTime> ArchivedDate { get; set; }

    public virtual ICollection<PollOption> PollOptions { get; set; }
}

这是一个PollOption.cs

   [Table("PollOptions")]
public class PollOption
{
    [Key]
    public int PollOptionID { get; set; }
    [DataType(DataType.Date)]
    public DateTime AddedDate { get; set; }
    public string AddedBy { get; set; }
    public string OptionText { get; set; }
    public int Votes { get; set; }
    public int PollID { get; set; }

    public virtual Poll Poll { get; set; }
}

在我的控制器中,我有一个操作方法,该方法返回Poll类型的模型

 public IActionResult ManagePoll()
        {
                               //public IQueryable<Poll> GetPolls
                                // {
                                //   get { return context.Polls; }
                                //  }

            var data = repository.GetPolls;   
            return View(data);
        }

和我对此操作方法的看法:

@model IEnumerable<Poll_Sys.Models.Poll>

@{
    ViewData["Title"] = "ManagePoll";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@foreach (var d in Model)
{
    @Html.Partial("_PollQuetions.cshtml", d)

}

,它有一个部分视图,名称为_PollQuetion.cshtml,该部分视图的代码为:

 @model Poll_Sys.Models.Poll

<div id="poll-" class="poll">
    <h2>@Model.QuestionText</h2>
    <ul>
        @foreach (var p in Model.PollOptions)
        {
            <li>
                <input type="radio"name="post-@p.PollOptionID" value="@p.PollOptionID" /> 
                <label for="option-@p.PollOptionID">@p.OptionText</label>
            </li>
        }
    </ul>
</div>

我在运行项目时收到此错误

****** NullReferenceException:对象引用未设置为对象的实例。****

AspNetCore.Views_Poll__PollQuetions.ExecuteAsync()
@model Poll_Sys.Models.Poll
<div id="poll-" class="poll">
    <h2>@Model.QuestionText</h2>
    <ul>
      @foreach (var p in Model.PollOptions)
        {
            <li>
                <input type="radio"name="post-@p.PollOptionID" value="@p.PollOptionID" /> 
                <label for="option-@p.PollOptionID">@p.OptionText</label>
            </li>
        } 

任何解决此问题的建议

0 个答案:

没有答案