Search code examples
c#asp.net-mvcviewicollection

Enumerated list of child class not working


I am creating a webpage for a help desk software where the top shows the information from the ticket class and then the bottom for the ticketNotes that are attached to the ticket class through an ICollection. Currently if I comment out the bottom, the top portion (the static table) works correctly. However, the bottom two tables that point to the ICollection are not working. Here is the error that I am getting:

Error I get on Page

Here is my view:

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

Here is my controller Code:

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

and here is my 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; }
    }
}

Solution

  • In your foreach loop

    @foreach (var item in Model.TicketNotes)
    

    item is already an instance of TicketNote, so

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

    fails because TicketNote does not have a property named TicketNotes

    It should be just

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

    For the table headings, you can use

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