如何在BreezeJs中正确发送动作参数和查询 [英] How to properly send action parameter along with query in BreezeJs

查看:53
本文介绍了如何在BreezeJs中正确发送动作参数和查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

getEntityList = function (predicate) {
  var query = new entityModel.EntityQuery().from("EntityList");
  if (predicate)
    query = query.where(predicate);
  return manager.executeQuery(query);
}

但是我想在返回任何可查询结果之前将附加参数传递给控制器​​操作:

But I want to pass additional parameter to controller action before any queryable result is returned:

[AcceptVerbs("GET")]
public IQueryable<Entity> EntityList(string actionParam) {
  //here goes logic that depends on actionParam
  //and returns IQueryable<Entity>
}

从文档中我们知道:


微风将查询转换为OData查询字符串,例如:

Breeze converts the query into an OData query string such as this one:


?$ filter = IsArchived%20eq%20false& $ orderby = CreatedAt

?$filter=IsArchived%20eq%20false&$orderby=CreatedAt


这是问题开始的地方。我应该如何构建查询以将参数传递给控制器​​操作?

This is where the problem starts. How should I build query to pass param to controller action?

getEntityList = function (predicate, actionParam) {
  var query = new entityModel.EntityQuery().from("EntityList");
  if (predicate)
    query = query.where(predicate);
  if(actionParam)
    // ???
  return manager.executeQuery(query);
}



我已经尝试设置以下路线:



I already tried setting route to:

routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{actionParam}",
            defaults: new { query = RouteParameter.Optional }
        );

并通过将 actionParam 应用于发件人部分,

var query = new entityModel.EntityQuery()
  .from("EntityList/" + encodeURIComponent(actionParam));

但是在某些特殊字符上编码失败,并且引发了错误的请求。

but encoding fails on some special chars and bad request is being thrown.

在这种情况下如何正确发送 actionParam ?请帮忙。

How can I properly send actionParam in such scenario? Please help.

推荐答案

从0.76.1版开始,您可以使用EntityQuery。 withParameters 方法可将其他参数传递给任何服务方法。因此,您现在可以构造如下查询,既可以传递参数,也可以使用微风的IQueryable支持。

As of v 0.76.1, you can use the EntityQuery.withParameters method to pass additional parameters to any service method. So you can now construct a query like the following that both passes parameters and uses breeze's IQueryable support.

EntityQuery.from("EmployeesFilteredByCountryAndBirthdate")
                 .withParameters({ BirthDate: "1/1/1960", Country: "USA" })
                 .where("LastName", "startsWith", "S")
                 .orderBy("BirthDate");

其中您的控制器方法如下所示:

where your controller method would look something like this:

[HttpGet]
public IQueryable<Employee> EmployeesFilteredByCountryAndBirthdate(DateTime birthDate, string country) {
      return ContextProvider.Context.Employees.Where(emp => emp.BirthDate >= birthDate && emp.Country == country);
}

API文档具有更多信息。

The API docs have more information.

这篇关于如何在BreezeJs中正确发送动作参数和查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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