How to use Kendo UI Grid with ToDataSourceResult(), IQueryable, ViewModel and AutoMapper?

前端 未结 6 1655
轻奢々
轻奢々 2021-02-02 01:00

What is the best approach to load/filter/order a Kendo grid with the following classes:

Domain:

public class Car
{
    public virtual in         


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-02 01:24

    Something about that seems weird. You told Kendo UI to make a grid for CarViewModel

    .Grid()
    

    and told it there is an IsActive column:

    columns.Bound(c => c.IsActive);
    

    but CarViewModel doesn't have a column by that name:

    public class CarViewModel
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual string IsActiveText { get; set; }
    }
    

    My guess is that Kendo is passing up the field name from the CarViewModel IsActiveText, but on the server you are running ToDataSourceResult() against Car objects (an IQueryable), which do not have a property by that name. The mapping happens after the filtering & sorting.

    If you want the filtering and sorting to happen in the database, then you would need to call .ToDataSourceResult() on the IQueryable before it runs against the DB.

    If you have already fetched all your Car records out of the DB, then you can fix this by doing your mapping first, then calling .ToDataSourceResult() on an IQueryable.

提交回复
热议问题