如何使用剑道UI网格ToDataSourceResult(),IQueryable的< T&GT ;,视图模型和AutoMapper? [英] How to use Kendo UI Grid with ToDataSourceResult(), IQueryable<T>, ViewModel and AutoMapper?

查看:809
本文介绍了如何使用剑道UI网格ToDataSourceResult(),IQueryable的< T&GT ;,视图模型和AutoMapper?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是加载/最好的方法过滤器/订购剑道网格以下类:

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

DataSourceResult

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过滤/订单,我得到以下信息:无效的属性或字段 - 'IsActiveText'类型:汽车。

Ok, grid load perfectly for the first time, but when i filter/order by IsActiveText, i get the following message: "Invalid property or field - 'IsActiveText' for type: Car".

什么是这个cenario最好的办法?

What is the best approach in this cenario?

推荐答案

有关的东西似乎不可思议。你叫剑道UI进行了 CarViewModel

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

.Grid<CarViewModel>()

和告诉它有一个 IsActive 列:

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

CarViewModel 不具备该名称的列:

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

我的猜测是,剑道从CarViewModel IsActiveText 向上传递的字段名,但在服务器上正在运行 ToDataSourceResult()对象(一个的IQueryable&LT;汽车&GT; ),不通过有一个属性名称。过滤和放大器后的映射发生;排序。

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.

如果你想过滤和排序在数据库中发生的,那么你就需要调用 .ToDataSourceResult()上的IQueryable它违背了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.

如果您已经取出所有的记载了数据库,那么你可以首先做你的映射解决这个问题,然后调用。 ToDataSourceResult()上的的IQueryable&LT; CarViewModel方式&gt;

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

这篇关于如何使用剑道UI网格ToDataSourceResult(),IQueryable的&LT; T&GT ;,视图模型和AutoMapper?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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