BinaryNexus BinaryNexus - 3 months ago 19
C# Question

Telerik NotSupportedException Grid using helper class

So I am trying to clean up some of my code because I dont want to add 30 extra lines of code for something I will be repeating often. But I cannot seem to get it to work properly.

I am using Entity Framework to get the records from my database and then trying to pass that IQueryable to a helper method to do the work and pass an IQueryable back to the initial variable. Then after that the method finishes with converting to a DataSourceResult for Telerik's Grid. The reason i think it is the helper method that is causing the issue is because I can put this all in the controller method and it works perfectly fine. But when I try to split it out, then it stops working and throws errors. So i must be doing something wrong.

Helper Method:

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


Controller that calls to the method:

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


Here is the output when the error occurs

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


And if it helps here is the razor code

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

Answer

My problem was that I needed to do eager loading in my helper class. So where my return is I should have had...

return detailedTickets.ToList();

Hope this helps someone.