如何将 Kendo UI Grid 与 ToDataSourceResult()、IQueryable<T>、ViewModel 和 AutoMapper 结合使用? [英] How to use Kendo UI Grid with ToDataSourceResult(), IQueryable<T>, ViewModel and AutoMapper?

查看:13
本文介绍了如何将 Kendo UI Grid 与 ToDataSourceResult()、IQueryable<T>、ViewModel 和 AutoMapper 结合使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下类加载/过滤/排序 Kendo 网格的最佳方法是什么:

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

域:

public class Car
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual bool IsActive { get; set; }
}

视图模型

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

AutoMapper

Mapper.CreateMap<Car, CarViewModel>()
      .ForMember(dest => dest.IsActiveText, 
                 src => src.MapFrom(m => m.IsActive ? "Yes" : "No"));

IQueryable

var domainList = RepositoryFactory.GetCarRepository().GetAllQueryable();

数据源结果

var dataSourceResult = domainList.ToDataSourceResult<Car, CarViewModel>(request, 
                          domain => Mapper.Map<Car, ViewModel>(domain));

网格

...Kendo()
  .Grid<CarViewModel>()
  .Name("gridCars")
  .Columns(columns =>
  {
     columns.Bound(c => c.Name);
     columns.Bound(c => c.IsActiveText);
  })
  .DataSource(dataSource => dataSource
     .Ajax()
     .Read(read => read.Action("ListGrid", "CarsController"))
  )
  .Sortable()
  .Pageable(p => p.PageSizes(true))

好的,网格第一次完美加载,但是当我通过 IsActiveText 过滤/排序时,我收到以下消息:

Ok, the grid loads perfectly for the first time, but when I filter/order by IsActiveText I get the following message:

无效的属性或字段 - 'IsActiveText' 类型:汽车

Invalid property or field - 'IsActiveText' for type: Car

在这种情况下最好的方法是什么?

What is the best approach in this scenario?

推荐答案

关于这个的事情似乎很奇怪.你告诉 Kendo UI 为 CarViewModel

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

.Grid<CarViewModel>()

并告诉它有一个 IsActive 列:

and told it there is an IsActive column:

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

CarViewModel 没有该名称的列:

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

我的猜测是 Kendo 正在传递 CarViewModel IsActiveText 中的字段名称,但在服务器上您正在针对 Car 运行 ToDataSourceResult()code> 对象(一个 IQueryable),它们不具有该名称的属性.映射发生在过滤之后排序.

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<Car>), which do not have a property by that name. The mapping happens after the filtering & sorting.

如果您希望在数据库中进行过滤和排序,那么您需要在 IQueryable 上调用 .ToDataSourceResult(),然后再针对 DB 运行.

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.

如果您已经从数据库中提取了所有 Car 记录,那么您可以通过先进行映射来解决此问题,然后在IQueryable.

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<CarViewModel>.

这篇关于如何将 Kendo UI Grid 与 ToDataSourceResult()、IQueryable&lt;T&gt;、ViewModel 和 AutoMapper 结合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆