我使用Kendo UI中的网格来显示实体列表。实体中的一个属性是Enum。我使用OData v4通过调用OData控制器从MVC5应用程序填充网格。使用的OData库是Microsoft.AspNet.Odata v5.6。以下是网格数据源的定义方式:
type: "odata-v4",
transport: {
read: {
url: "/odata/Groups/",
dataType: "json"
}
},而控制器操作非常简单,如下所示
[Authorize]
[HttpGet]
[EnableQuery(PageSize = 20)]
public IQueryable<Group> GetGroups()
{
return _GroupService.GetGroups();
}集团实体定义为
public class Group
{
[Required(ErrorMessageResourceName = "FieldRequired", ErrorMessageResourceType = typeof(Resources.BusinessEntitiesResources))]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int ID { get; set; }
[Required(ErrorMessageResourceName = "FieldRequired", ErrorMessageResourceType = typeof(Resources.BusinessEntitiesResources))]
[StringLength(100)]
public string Name { get; set; }
public EnumDepositMethod? DepositMethod { get; set; }
}现在,按DepositMethod列过滤不起作用。错误消息是关于无法比较枚举和字符串类型的值。以下是发送到服务器的有效负载:
http://localhost:49680/odata/Groups?format=json&top=20&filter=DepositMethod+eq+DebitCard&count=true据我所知,kendo网格生成的查询字符串不能更改,因此无法在发送到服务器的值前加上枚举的完全限定名称。有没有办法让OData在服务器端将值解释为枚举,或者让网格将"filter by“值正确地前缀发送到服务器?
感谢您抽出时间来阅读这篇文章。
发布于 2015-08-26 16:59:28
我刚刚发现有一种方法可以使OData URI解析器在解释枚举类型的值时不那么严格。在配置上调用EnableEnumPrefixFree将使解析器尝试解析该值,并将其与枚举的成员之一进行匹配。OData配置现在如下所示:
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes(); //This has to be called before the following OData mapping and before the WebApi mapping
config.MapODataServiceRoute("ODataRoute", "odata", GetEdmModel());
config.EnableEnumPrefixFree(enumPrefixFree: true);
}发布于 2015-12-10 06:30:31
是的,有一种方法可以在dataSource.transport.parameterMap函数中更改网格生成的过滤器。我使用的一种变通方法是在kendo数据源对象的parameterMap函数中使用正则表达式replace。请参见下面的示例。
parameterMap: function (data,operation) {
if (operation == "read") {
var paramMap = kendo.data.transports.odata.parameterMap(data);
if (paramMap.$filter) {
paramMap.$filter = paramMap.$filter.replace(/Level\s+(.+)\s+'(.+)'/, "Level $1 System.Diagnostics.Tracing.EventLevel'$2'");
}
return paramMap;
}https://stackoverflow.com/questions/32183250
复制相似问题