动态ViewModel MVC ASP中的空数据

时间:2019-01-17 18:35:12

标签: asp.net-mvc-5

我正在尝试使用Expandos方法查找关系数据(一对多),但是在注释的空引用上却遇到了错误:

 Comment = context.TicketComments.Find(TicketID).Comment;

谁能告诉我我哪里出问题了?

TicketController:

public ActionResult ViewTickets(int TicketID)
    {

        Ticket Ticket = context.Tickets.SingleOrDefault(x => x.TicketID == TicketID);

        CommentViewModel TicketVM = new CommentViewModel();

        TicketVM.TicketID = Ticket.TicketID;
        TicketVM.Title = Ticket.Title;

        TicketVM.OwnerName = Ticket.TicketOwner.OwnerName;
        TicketVM.Status = Ticket.TicketStatus.Status;
        TicketVM.Type = Ticket.TicketType.Type;
        TicketVM.Priority = Ticket.TicketPriority.Priority;
        TicketVM.Description = Ticket.Description;
        TicketVM.Comment = GetComment(Ticket.TicketID);


        return View(TicketVM);
    }

    private string GetComment(int? TicketID)
    {
        string Comment;

        Comment = context.TicketComments.Find(TicketID).Comment;

        return Comment;
    }

CommentViewModel:

   namespace ITHelpdesk.Areas.Support.ViewModels
    {
       public class CommentViewModel
         {
    public int TicketID { get; set; }
    public string Title { get; set; }
    public System.DateTime TimeStamp { get; set; }
    public string Description { get; set; }
    public string OwnerName { get; set; }
    public string Status { get; set; }
    public string Type { get; set; }
    public string Priority { get; set; }
    public int TicketCommentID { get; set; }
    public string Comment { get; set; }

    }

   }

机票型号:

   namespace ITHelpdesk.Areas.Support.Models
     {

       public class Ticket
       {
    [Key]
    public int TicketID { get; set; }
    [Required]
    [StringLength(100, MinimumLength = 2)]
    public string Title { get; set; }
    [DataType(DataType.DateTime)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:ddd, d MMMM yyyy, hh.mm tt}")]
    public DateTime? TimeStamp { get; set; }
    [Required]
    [StringLength(maximumLength:255, MinimumLength = 2)]
    [DataType(DataType.MultilineText)]
    public string Description { get; set; }


    public int TicketOwnerID { get; set; }
    public int TicketStatusID { get; set; }
    public int TicketTypeID { get; set; }
    public int TicketPriorityID { get; set; }

    public virtual TicketOwner TicketOwner { get; set; }
    public virtual TicketStatus TicketStatus { get; set; }
    public virtual TicketPriority TicketPriority { get; set; }
    public virtual TicketType TicketType { get; set; }

    public virtual ICollection<TicketComment> TicketComments { get; set;           }


     }

评论模型:

 namespace ITHelpdesk.Areas.Support.Models
  {
     public class TicketComment
      {

    [Key]
    public int TicketCommentID { get; set; }

    [Required]
    public int CommentOwnerID { get; set; }
    [Required]
    [DataType(DataType.DateTime)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:ddd, d MMMM yyyy, hh.mm tt}")]
    public DateTime? CommentDate { get; set; }

    [Required]
    [StringLength(int.MaxValue, MinimumLength = 1)]
    [DataType(DataType.MultilineText)]
    public string Comment { get;  set; }
    public int TicketID { get; set; }
    public virtual Ticket Ticket { get; set; }


    }
  }

0 个答案:

没有答案