所以我正在尝试清理我的一些代码,因为我不想为我经常重复的内容添加30行代码。但我似乎无法让它正常工作。
我正在使用Entity Framework从我的数据库中获取记录,然后尝试将该IQueryable传递给辅助方法来完成工作并将IQueryable传递回初始变量。然后,该方法完成转换为Telerik网格的DataSourceResult。我认为是导致问题的辅助方法的原因是因为我可以把这一切都放在控制器方法中并且它工作得非常好。但是当我试图将其拆分出来时,它就会停止工作并抛出错误。所以我一定是做错了。
帮手方法:
public static IQueryable<DetailedTicketModel> GetDetailedTicketModels(IQueryable<TICKET> tickets)
{
using (var ctx = new GuardianContext())
{
var detailedTickets = tickets.Select(v => new DetailedTicketModel()
{
Id = v.ID,
RequesterId = v.REQUESTER_ID,
RequesterName = v.REQUESTER_NAME,
Phone = v.PHONE,
Location = v.LOCATION,
Source = v.SOURCE,
PersonAssigned = v.PERSON_ASSIGNED,
Created = v.CREATED,
Updated = v.UPDATED,
DeptId = v.DEPT_ID,
DeptName = ctx.DEPARTMENTS.FirstOrDefault(t => t.ID == v.DEPT_ID).NAME,
TopicId = v.TOPIC_ID,
TopicName = ctx.TICKET_TOPICS.FirstOrDefault(t => t.ID == v.TOPIC_ID).NAME,
StatusId = v.STATUS_ID,
StatusName = ctx.TICKET_STATUSES.FirstOrDefault(t => t.ID == v.STATUS_ID).NAME,
PriorityId = v.PRIORITY_ID,
PriorityName = ctx.TICKET_PRIORITIES.FirstOrDefault(t => t.ID == v.PRIORITY_ID).NAME,
PriorityHexColor = ctx.TICKET_PRIORITIES.FirstOrDefault(t => t.ID == v.PRIORITY_ID).HEX_COLOR,
TicketEvents = ctx.TICKET_EVENTS.Where(t => t.TICKET_ID == v.ID).OrderBy(t => t.CREATED),
RequestBody =
ctx.TICKET_EVENTS.OrderBy(t => t.CREATED).FirstOrDefault(t => t.TICKET_ID == v.ID).BODY,
RequestFormat =
ctx.TICKET_EVENTS.OrderBy(t => t.CREATED).FirstOrDefault(t => t.TICKET_ID == v.ID).FORMAT,
});
return detailedTickets;
}
}
调用方法的控制器:
public ActionResult UnassignedTickets_Read([DataSourceRequest]DataSourceRequest request)
{
Debug.WriteLine("UnassignedTickets_Read");
using (var ctx = new GuardianContext())
{
var detailedTickets =
TicketHelper.GetDetailedTicketModels(ctx.TICKETS.Where(v => v.PERSON_ASSIGNED == null));
//PROBLEM HAPPENS AT THE NEXT STATEMENT
var result = detailedTickets.ToDataSourceResult(request, ticket => new
{
ticket.Id,
ticket.RequesterId,
ticket.RequesterName,
ticket.Created,
ticket.RequestBody,
ticket.RequestFormat
});
return Json(result);
}
}
发生错误时的输出
A first chance exception of type 'System.NotSupportedException' occurred in EntityFramework.dll
A first chance exception of type 'System.NotSupportedException' occurred in EntityFramework.dll
A first chance exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll
A first chance exception of type 'System.NotSupportedException' occurred in System.Web.Mvc.dll
A first chance exception of type 'System.NotSupportedException' occurred in System.Web.Mvc.dll
如果它有帮助,那就是剃刀代码
@if (ViewBag.UnassignedTicketsAvailable)
{
<h3 class="page-header">Unassigned Tickets</h3>
@(Html.Kendo().Grid<Guardian.ViewModels.DetailedTicketModel>()
.Name("unassigned_grid")
.Columns(columns =>
{
columns.Bound(ticket => ticket.Id).Visible(false);
columns.Bound(ticket => ticket.RequesterId);
columns.Bound(ticket => ticket.RequesterName);
columns.Bound(ticket => ticket.Created);
})
.DataSource(dataSource => dataSource.Ajax().Read(read => read.Action("UnassignedTickets_Read", "Ticket"))
)
.ClientDetailTemplateId("client-template")
.Sortable()
.Pageable()
.Filterable()
)
}
<script id="client-template" type="text/kendo-tmpl">
@(Html.Raw("<div style\"padding: 0.4em;\">#=RequestBody#</div>"))
</script>
<script>
function dataBound() {
this.expandRow(this.tbody.find("tr.k-master-row").first());
}
</script>
答案 0 :(得分:0)
我的问题是我需要在帮助程序类中进行急切加载。所以我回归的地方应该是......
return detailedTickets.ToList();
希望这有助于某人。