bitshift
bitshift

Reputation: 6852

kendo ui autocomplete - how to pass parameters to controller method to filter results

How can I pass parameters that reference other elements in the UI (such as textbox values, radio buttons etc) to the controller that gets called to fetch the data for autocomplete?

eg. from the example in the docs, when I call the Home controller GetProducts action, how can I send some parameters along with the call?
Or even better, post a json object that contains values referencing data from 2-3 other widgets in the ui?
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.
       })
    )

Upvotes: 0

Views: 2798

Answers (1)

Sean Ch
Sean Ch

Reputation: 614

You could pass additional data either using object route values E.g.

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

OR via the Data method,

which specifies a JavaScript function, which will return the additional parameters. E.g.

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

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

or Via Template Delegate

  //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>))   

In all cases, you should add additional parameters to your Action. E.g.

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

Upvotes: 1

Related Questions