【问题标题】:kendo ui autocomplete - how to pass parameters to controller method to filter resultskendo ui autocomplete - 如何将参数传递给控制器​​方法以过滤结果
【发布时间】:2017-10-07 06:24:46
【问题描述】:

如何将引用 UI 中其他元素(例如文本框值、单选按钮等)的参数传递给被调用以获取自动完成数据的控制器?

例如。从文档中的示例中,当我调用 Home 控制器 GetProducts 操作时,如何随调用一起发送一些参数?
或者更好的是,发布一个 json 对象,其中包含引用来自 ui 中 2-3 个其他小部件的数据的值?
https://docs.telerik.com/aspnet-mvc/helpers/autocomplete/overview#ajax-binding

  @(Html.Kendo().AutoComplete()
      .Name("productAutoComplete") //The name of the AutoComplete is mandatory. It specifies the "id" attribute of the widget.
      .DataTextField("ProductName") //Specify which property of the Product to be used by the AutoComplete.
      .DataSource(source =>
       {
          source.Read(read =>
          {
               read.Action("GetProducts", "Home"); //Set the Action and Controller names.
          })
          .ServerFiltering(true); //If true, the DataSource will not filter the data on the client.
       })
    )

【问题讨论】:

  • -Bitshift: - 你有机会调查答案吗?只是想知道,因为您最后一次看到的状态是 1 小时

标签: asp.net-mvc kendo-asp.net-mvc


【解决方案1】:

您可以使用对象路由值传递其他数据 例如

.Read(read => read.Action("Products_Read", "Home", new { name = "test", id = 2 }))

或 通过 Data 方法,

指定一个 JavaScript 函数,该函数将返回附加参数。 例如

.Read(read => read.Action("Products_Read", "Home").Data("additionalInfo"))

 function additionalInfo() {
  return {
    name: "test",
    id: 2
  }
 }

或通过模板委托

  //By Template Delegate
  Read(read => read.Action("Products_Read", "Home").Data(@<text>
        function() {
            //pass parameters to the Read method
            return {
                name: "test",
                id: $("#search").val()
            }
        }
          </text>))   

在所有情况下,您都应该向您的操作添加额外的参数。 例如

 public ActionResult Products_Read([DataSourceRequest] DataSourceRequest request, string name, int id) {...}

【讨论】:

  • 是的,我看过那个例子,这就是我所指的。我的问题与行动的签名有关。而不是传递名称、id 等。这些“额外”参数可以作为单个 JSON 对象传递吗?这样当需要传递更多数据时,我不必更改方法的签名。
  • 好的,你可以在一个对象中接收数据。名称应该完全匹配。我遇到了同样的情况,它对我有用。您的类和返回语句中的属性名称必须相同。
猜你喜欢
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 2013-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多