gpt4 book ai didi

c# - 如何将 Kendo UI Grid 与 ToDataSourceResult()、IQueryable、ViewModel 和 AutoMapper 一起使用?

转载 作者:可可西里 更新时间:2023-11-01 08:36:33 26 4
gpt4 key购买 nike

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

域:

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

View 模型

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

自动映射器

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

可查询

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 过滤/排序时,我收到以下消息:

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

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

最佳答案

我不喜欢 Kendo 实现“DataSourceRequestAttribute”和“DataSourceRequestModelBinder”的方式,但那是另一回事了。

要能够按“扁平化”对象的 VM 属性进行过滤/排序,请尝试以下操作:

领域模型:

public class Administrator
{
public int Id { get; set; }

public int UserId { get; set; }

public virtual User User { get; set; }
}

public class User
{
public int Id { get; set; }

public string UserName { get; set; }

public string Email { get; set; }
}

查看模型:

public class AdministratorGridItemViewModel
{
public int Id { get; set; }

[Displaye(Name = "E-mail")]
public string User_Email { get; set; }

[Display(Name = "Username")]
public string User_UserName { get; set; }
}

扩展:

public static class DataSourceRequestExtensions
{
/// <summary>
/// Enable flattened properties in the ViewModel to be used in DataSource.
/// </summary>
public static void Deflatten(this DataSourceRequest dataSourceRequest)
{
foreach (var filterDescriptor in dataSourceRequest.Filters.Cast<FilterDescriptor>())
{
filterDescriptor.Member = DeflattenString(filterDescriptor.Member);
}

foreach (var sortDescriptor in dataSourceRequest.Sorts)
{
sortDescriptor.Member = DeflattenString(sortDescriptor.Member);
}
}

private static string DeflattenString(string source)
{
return source.Replace('_', '.');
}
}

属性:

[AttributeUsage(AttributeTargets.Method)]
public class KendoGridAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);

foreach (var sataSourceRequest in filterContext.ActionParameters.Values.Where(x => x is DataSourceRequest).Cast<DataSourceRequest>())
{
sataSourceRequest.Deflatten();
}
}
}

Ajax 数据加载的 Controller 操作:

[KendoGrid]
public virtual JsonResult AdministratorsLoad([DataSourceRequestAttribute]DataSourceRequest request)
{
var administrators = this._administartorRepository.Table;

var result = administrators.ToDataSourceResult(
request,
data => new AdministratorGridItemViewModel { Id = data.Id, User_Email = data.User.Email, User_UserName = data.User.UserName, });

return this.Json(result);
}

关于c# - 如何将 Kendo UI Grid 与 ToDataSourceResult()、IQueryable<T>、ViewModel 和 AutoMapper 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16484417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com