枚举子列表 class 不工作

Enumerated list of child class not working

我正在为帮助台软件创建一个网页,其中顶部显示来自工单 class 的信息,然后底部显示通过 ICollection 附加到工单 class 的工单注释.目前,如果我注释掉底部,则顶部(静态 table)可以正常工作。但是,指向 ICollection 的底部两个 table 不起作用。这是我收到的错误:

这是我的观点:

    @model HelpDesk.Model.Ticket

@{
    ViewBag.Title = "Ticket# " + Html.DisplayFor(model => model.TicketNumber);
}

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.CategoryId)
        </th>
        <th>
            @Html.DisplayFor(model => model.Category.CategoryName)
        </th>
    </tr>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.OpenUserId)
        </th>
        <th>
            @Html.DisplayFor(model => model.OpenUser.FullName)
        </th>
    </tr>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.OpenDate)
        </th>
        <th>
            @Html.DisplayFor(model => model.OpenDate)
        </th>
    </tr>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.TechnicianId)
        </th>
        <th>
            @Html.DisplayFor(model => model.Technician.FullName)
        </th>
    </tr>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.TicketStatusId)
        </th>
        <th>
            @Html.DisplayFor(model => model.TicketStatus.StatusDescription)
        </th>
    </tr>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.CloseDate)
        </th>
        <th>
            @Html.DisplayFor(model => model.CloseDate)
        </th>
    </tr>
    <tr></tr>
</table>

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.TicketNotes.Note)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TicketNotes.TicketNoteDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TicketNotes.UserNote.FullName)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model.TicketNotes)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.TicketNotes.Note)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TicketNotes.TicketNoteDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TicketNotes.UserNote.FullName)
            </td>
        </tr>
    }

</table>

这是我的控制器代码:

public ActionResult EditUserTicket(Guid id)
        {
            Ticket ticket = tickets.GetById(id);

            var model = db.Tickets
                    .Include(t => t.TicketNotes)
                    .Where(t => t.TicketId == id).First();

            return View(model);
        }

这是我的模型:

namespace HelpDesk.Model
{
    public class Ticket
    {
        public Ticket()
        {
            this.TicketNotes = new HashSet<TicketNote>();
        }
        public Guid TicketId { get; set; }

        [Display(Name = "#")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Required]
        public int TicketNumber { get; set; }

        [ForeignKey("Category")]
        [Required]
        public Guid CategoryId { get; set; }

        public virtual Category Category { get; set; }

        [ForeignKey("OpenUser")]
        [Required]
        public Guid OpenUserId { get; set; }

        [Display(Name = "Ticket Owner")]
        public virtual User OpenUser { get; set; }

        [Display(Name = "Opened")]
        [DataType(DataType.Date)]
        [Required]
        public DateTime OpenDate { get; set; }

        [ForeignKey("Technician")]
        [Required]
        public Guid TechnicianId { get; set; }

        [Display(Name = "Technician")]
        public virtual User Technician { get; set; }

        [ForeignKey("TicketStatus")]
        [Required]
        public Guid TicketStatusId { get; set; }

        public virtual TicketStatus TicketStatus { get; set; }

        [Display(Name = "Closed")]
        [DataType(DataType.Date)]
        public Nullable<DateTime> CloseDate { get; set; }

        [Display(Name = "Note")]
        public virtual ICollection<TicketNote> TicketNotes { get; set; }
        //public virtual ICollection<TicketSubscription> TicketSubscriptions { get; set; }
    }
}

在你的 foreach 循环中

@foreach (var item in Model.TicketNotes)

item 已经是 TicketNote 的实例,所以

@Html.DisplayFor(modelItem => item.TicketNotes.Note)

失败,因为 TicketNote 没有名为 TicketNotes

的 属性

应该只是

@Html.DisplayFor(m => item.Note)

对于 table 个标题,您可以使用

@Html.DisplayNameFor(model => model.TicketNotes[0].Note)