This is a migrated thread and some comments may be shown as answers.

Grid Helper Class Error Out (NotSupportedException)

3 Answers 19 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joshua
Top achievements
Rank 2
Joshua asked on 17 Mar 2015, 06:30 PM
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 here is the Razor code if it helps any...

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

Please help. Thanks in advance.

3 Answers, 1 is accepted

Sort by
0
Joshua
Top achievements
Rank 2
answered on 18 Mar 2015, 06:07 PM
I was not eagerly loading my results from the Helper class. When I added .ToList() to my return value, it works. Derp! Hopefully this helps someone.
0
Jerome
Top achievements
Rank 1
answered on 08 Feb 2021, 03:21 PM

Hi,

I have the same problem and solution "ToList" is not acceptable due to of lack of performance with a lot of lines.

Do you have another solution?

0
Eyup
Telerik team
answered on 10 Feb 2021, 09:36 AM

Hi Jerome,

 

In most cases the .ToList() option is the only solution. However, you can also check these suggestions:

1. Adding a Schema and Model to the DataSource instance as demonstrated here:
https://demos.telerik.com/aspnet-mvc/grid/editing-custom

2. Custom Server Binding:
https://demos.telerik.com/aspnet-mvc/grid/customajaxbinding

And if you have cautions about performance, you can also implement the Virtualization feature:
https://demos.telerik.com/aspnet-mvc/grid/virtualization-remote-data

I hope this will help remedy the situation.

 

Regards,
Eyup
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
Joshua
Top achievements
Rank 2
Answers by
Joshua
Top achievements
Rank 2
Jerome
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or