Kendo.Mvc.CompositeFilterDescriptor problem with custom binding

2 posts, 0 answers
  1. Alan Mosley
    Alan Mosley avatar
    200 posts
    Member since:
    Apr 2010

    Posted 08 Jul 2015 Link to this post

     When using "Filter Multi Checkboxes "  I get an error saying cannot convert CompositeFilterDescriptor

     to FilterDescriptor . on this line "For Each filterDescriptor As FilterDescriptor In request.Filters"

    I understand the error, but  how to get around it,

    thanks

     

    Imports Kendo.Mvc
    Imports System.ComponentModel
    Imports System.Collections.ObjectModel
    Imports System.Data.Entity
     
    Namespace Models.Students
        Public Class StudentGridModel
            Public Property PageSize As Integer = 25
            Private _Students As ReadOnlyCollection(Of BO.Models.Students.Student)
            <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")> Public Property Students As ReadOnlyCollection(Of BO.Models.Students.Student)
                Get
                    Return _Students
                End Get
                Set(value As ReadOnlyCollection(Of BO.Models.Students.Student))
                    _Students = value
                End Set
            End Property
     
            Public Property Total As Integer
     
            Sub New()
                Using db As EF.GrasshopperEntities = New EF.GrasshopperEntities
                    Me._Students = New ReadOnlyCollection(Of BO.Models.Students.Student)(db.Students.Include(Function(i) i.Franchisee).OrderBy(Function(o) o.StudentId).Take(Me.PageSize).AsEnumerable.Select(Function(s) New BO.Models.Students.Student With {
                                    .FirstName = s.FirstName,
                                    .FranchiseeName = s.Franchisee.Name,
                                    .LastName = s.LastName,
                                    .Age = s.Age,
                                    .Birthday = s.Birthday,
                                    .Female = s.Female,
                                    .Suburb = s.Suburb,
                                    .PhoneNumber = s.PhoneNumber,
                                    .MobileNumber = s.MobileNumber,
                                    .Email = s.Email,
                                    .TotalSales = s.Sales(),
                                    .ProductSales = s.Sales(Function(t) Not t.ProductId.Equals(0)),
                                    .EnrolmentSales = s.Sales(Function(t) Not t.EnrolmentId.Equals(0)),
                                    .EventSales = s.Sales(Function(t) Not t.StudentEventId.Equals(0)),
                                    .StudentId = s.StudentId}).ToList)
                    Me.Total = db.Students.Count
                End Using
     
            End Sub
     
            Sub New(request As UI.DataSourceRequest)
                Using db As EF.GrasshopperEntities = New EF.GrasshopperEntities
                    Me._Students = New ReadOnlyCollection(Of BO.Models.Students.Student)(GetData(request, Me.Total))
                End Using
            End Sub
     
            ''' <summary>
            ''' Reterns student data for kendo grid
            ''' </summary>
            ''' <param name="request">Kendo.Mvc.UI.DataSourceRequest</param>
            ''' <param name="Total">Integer</param>
            ''' <returns>ReadOnlyCollection(Of BO.Models.Students.students)</returns>
            ''' <remarks></remarks>
            Private Shared Function GetData(ByVal request As Kendo.Mvc.UI.DataSourceRequest, ByRef total As Integer) As ReadOnlyCollection(Of BO.Models.Students.Student)
                Using db As EF.GrasshopperEntities = New EF.GrasshopperEntities
                    Dim data As IQueryable(Of EF.Student) = db.Students.Include(Function(i) i.Franchisee)
                    If Not request.Sorts Is Nothing AndAlso request.Sorts.Any Then
                        For Each sortDescriptor As SortDescriptor In request.Sorts
                            data = SortStudent(sortDescriptor, data)
                        Next
                    Else
                        data = data.OrderBy(Function(o) o.StudentId)
                    End If
     
                    If request.Filters.Any() Then
                        Dim filterValue As String = String.Empty
                        For Each filterDescriptor As FilterDescriptor In request.Filters
     
                            Select Case filterDescriptor.Member
                                Case "FranchiseeName"
                                    filterValue = CStr(filterDescriptor.ConvertedValue)
                                    Select Case filterDescriptor.Operator
                                        Case FilterOperator.Contains
                                            data = data.Where(Function(w) w.Franchisee.Name.Contains(filterValue))
                                        Case FilterOperator.DoesNotContain
                                            data = data.Where(Function(w) Not w.Franchisee.Name.Contains(filterValue))
                                        Case FilterOperator.EndsWith
                                            data = data.Where(Function(w) w.Franchisee.Name.EndsWith(filterValue))
                                        Case FilterOperator.IsEqualTo
                                            data = data.Where(Function(w) w.Franchisee.Name.Equals(filterValue))
                                        Case FilterOperator.IsNotEqualTo
                                            data = data.Where(Function(w) Not w.Franchisee.Name.Equals(filterValue))
                                        Case FilterOperator.StartsWith
                                            data = data.Where(Function(w) w.Franchisee.Name.StartsWith(filterValue))
                                    End Select
                            End Select
                        Next
                    End If
                    If request.Filters.Any() Then
                        data = data.Where(ExpressionBuilder.Expression(Of EF.Student)(request.Filters))
                    End If
                    total = data.Count
     
                    If request.PageSize > 0 Then
                        data = data.Skip((request.Page - 1) * request.PageSize)
                        data = data.Take(request.PageSize)
                    End If
                    Return New ReadOnlyCollection(Of BO.Models.Students.Student)(data.AsEnumerable.Select(Function(s) New BO.Models.Students.Student With {
                                            .FranchiseeName = s.Franchisee.Name,
                                            .FirstName = s.FirstName,
                                            .LastName = s.LastName,
                                            .Age = s.Age,
                                            .Birthday = s.Birthday,
                                            .Female = s.Female,
                                            .Suburb = s.Suburb,
                                            .PhoneNumber = s.PhoneNumber,
                                            .MobileNumber = s.MobileNumber,
                                            .Email = s.Email,
                                            .TotalSales = s.Sales(),
                                            .ProductSales = s.Sales(Function(t) Not t.ProductId.Equals(0)),
                                            .EnrolmentSales = s.Sales(Function(t) Not t.EnrolmentId.Equals(0)),
                                            .EventSales = s.Sales(Function(t) Not t.StudentEventId.Equals(0)),
                                            .StudentId = s.StudentId}).ToList)
                End Using
     
            End Function
     
            Private Shared Function SortStudent(sortDescriptor As SortDescriptor, data As IQueryable(Of EF.Student)) As IQueryable(Of EF.Student)
                If sortDescriptor.SortDirection.Equals(ListSortDirection.Ascending) Then
                    Select Case sortDescriptor.Member
                        Case "StudentId"
                            data = data.OrderBy(Function(o) o.StudentId)
                        Case "FranchiseeName"
                            data = data.OrderBy(Function(o) o.Franchisee.Name)
                        Case "FirstName"
                            data = data.OrderBy(Function(o) o.FirstName)
                        Case "LastName"
                            data = data.OrderBy(Function(o) o.LastName)
                        Case "Age"
                            data = data.OrderBy(Function(o) o.Birthday)
                        Case "Female"
                            data = data.OrderBy(Function(o) o.Female)
                        Case "Suburb"
                            data = data.OrderBy(Function(o) o.Suburb)
                    End Select
                Else
                    Select Case sortDescriptor.Member
                        Case "StudentId"
                            data = data.OrderByDescending(Function(o) o.StudentId)
                        Case "FranchiseeName"
                            data = data.OrderByDescending(Function(o) o.Franchisee.Name)
                        Case "FirstName"
                            data = data.OrderByDescending(Function(o) o.FirstName)
                        Case "LastName"
                            data = data.OrderByDescending(Function(o) o.LastName)
                        Case "Age"
                            data = data.OrderByDescending(Function(o) o.Birthday)
                        Case "Female"
                            data = data.OrderByDescending(Function(o) o.Female)
                        Case "Suburb"
                            data = data.OrderByDescending(Function(o) o.Suburb)
                    End Select
                End If
                Return data
            End Function
        End Class
    End Namespace
     

     

     

  2. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 13 Jul 2015 Link to this post

    Hello Alan,

    You need to check this code

    For Each filterDescriptor As FilterDescriptor In request.Filters

    to

    For Each filterDescriptor As IFilterDescriptor In request.Filters

    Then you need to make a check if the filterDescriptor is an instance of CompositeFilterDescriptor and if it is iterate over its Filters property.

    Regards,
    Atanas Korchev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top