В настоящее время я работаю над системой справки и записываю представление, чтобы просмотреть все билеты для пользователя.Просмотр данных из другой модели приводит к тому, что объект DynamicProxies отображается
Билет состоит из родительских данных, таких как категория билетов, открытый пользователь, техник и т. Д., А затем есть ребенок с билетами. Первая записка о билете (которая добавляется при первоначальном создании) - это описание билета. В таблице билетов на мой взгляд я хотел бы показать только одну заметку (описание билета) для каждого билета. Вот мой контроллер:
public ActionResult Index()
{
var model = from t in db.Tickets
join tn in db.TicketNotes on t.TicketId equals tn.TicketId
where t.OpenUserId == new Guid("00000000-0000-0000-0000-000000000000")
orderby t.OpenDate
select t;
return View(model);
}
Мои ViewModel:
namespace HelpDesk.WebUI.ViewModel
{
public class UserTickets
{
public int TicketNumber { get; set; }
public Guid CategoryId { get; set; }
public virtual Category Category { get; set; }
public Guid OpenUserId { get; set; }
public virtual User OpenUser { get; set; }
public DateTime OpenDate { get; set; }
public Guid TechnicianId { get; set; }
public virtual User Technician { get; set; }
public Guid TicketStatusId { get; set; }
public virtual TicketStatus TicketStatus { get; set; }
public Nullable<DateTime> CloseDate { get; set; }
public string Note { get; set; }
}
}
Мои модели:
namespace HelpDesk.Model
{
public class Ticket
{
public Ticket()
{
this.TicketNotes = new HashSet<TicketNote>();
}
public Guid TicketId { get; set; }
[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; }
public virtual User OpenUser { get; set; }
[DataType(DataType.Date)]
[Required]
public DateTime OpenDate { get; set; }
[ForeignKey("Technician")]
[Required]
public Guid TechnicianId { get; set; }
public virtual User Technician { get; set; }
[ForeignKey("TicketStatus")]
[Required]
public Guid TicketStatusId { get; set; }
public virtual TicketStatus TicketStatus { get; set; }
[DataType(DataType.Date)]
public Nullable<DateTime> CloseDate { get; set; }
public virtual ICollection<TicketNote> TicketNotes { get; set; }
//public virtual ICollection<TicketSubscription> TicketSubscriptions { get; set; }
}
}
namespace HelpDesk.Model
{
public class TicketNote
{
public Guid TicketNoteId { get; set; }
[ForeignKey("Ticket")]
[Required]
public Guid TicketId { get; set; }
public virtual Ticket Ticket { get; set; }
public string Note { get; set; }
[Display(Name = "Attachment")]
public string AttachmentPath { get; set; }
[MaxLength(255)]
public string AttachmentName { get; set; }
[ForeignKey("UserNote")]
[Required]
public Guid UserNoteId { get; set; }
public virtual User UserNote { get; set; }
[DataType(DataType.Date)]
[Required]
public DateTime TicketNoteDate { get; set; }
[Display(Name = "Private Note")]
[Required]
public bool PublicFlag { get; set; }
public bool Delete { get; set; }
}
}
А вот мое мнение:
@model IEnumerable<HelpDesk.Model.Ticket>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Category.CategoryName)
</th>
<th>
@Html.DisplayNameFor(model => model.OpenUser.FullName)
</th>
<th>
@Html.DisplayNameFor(model => model.Technician.FullName)
</th>
<th>
@Html.DisplayNameFor(model => model.TicketStatus.StatusDescription)
</th>
<th>
@Html.DisplayNameFor(model => model.TicketNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.OpenDate)
</th>
<th>
@Html.DisplayNameFor(model => model.CloseDate)
</th>
<th>
@Html.DisplayNameFor(model => model.TicketNotes)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Category.CategoryName)
</td>
<td>
@Html.DisplayFor(modelItem => item.OpenUser.NTUserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Technician.NTUserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.TicketStatus.StatusDescription)
</td>
<td>
@Html.DisplayFor(modelItem => item.TicketNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.OpenDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.CloseDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.TicketNotes.OrderBy(t=t.TicketId).First().TicketText)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.TicketId }) |
@Html.ActionLink("Details", "Details", new { id=item.TicketId }) |
</td>
</tr>
}
</table>
Все работает за исключением билетная записка, которая вытягивается из Tick Модель etNote. Для этой области я получаю
System.Data.Entity.DynamicProxies.Ticket_BA0BCDE55BCBE6BA2FE66FAD697C8D2C0D7AAD7C5F797406642720CB57DA2A89
Как предположил один я изменил код в моем контроллере быть:
public ActionResult Index()
{
var model = db.Tickets
.Include(t=>t.TicketNotes)
.Where(t.OpenUserId == new Guid("00000000-0000-0000-0000-000000000000"))
.OrderBy(t => t.OpenDate);
return View(model);
}
и мои просмотреть, как предложено, но я получаю ошибку на этой линии на вид:
@Html.DisplayFor(modelItem => item.TicketNotes.OrderBy(t=t.TicketId).First().TicketText)
Говорят, что «Имя« t »не существует в текущем контексте.
Ваш запрос Linq немного бессмысленен, поскольку вы выбираете только билет, а не билет. Эти ноты вытягиваются позже с ленивой загрузкой. – DavidG