列举的子类无法正常工作

时间:2015-06-08 13:44:23

标签: c# asp.net-mvc view icollection

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

Error I get on Page

以下是我的观点:

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

1 个答案:

答案 0 :(得分:0)

foreach循环中

@foreach (var item in Model.TicketNotes)

item已经是TicketNote的一个实例,所以

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

失败,因为TicketNote没有名为TicketNotes

的属性

应该只是

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

对于表格标题,您可以使用

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