Send Additional Parameter in Kendo Grid Read Action

大城市里の小女人 提交于 2019-12-03 05:03:34

If the additional data is known at server-side you should use the overload of the Action method which accepts route values:

.DataSource(dataSource => dataSource.Server()
   .Read(read => read.Action("Read", "Home", 
        new { AdditionalParam = ViewData["AdditionalParam"] }))
)

If this additional data is known at the client-side only you should use the Data method:

.DataSource(dataSource => dataSource.Ajax()
   .Read(read => read
      .Action("Read", "Home")
      .Data("additionalData")
  )
)
<script>
 function additionalData() {
     return {
         AdditionalParam: $("#search").val()
     };
 }
</script>

You can try this;

.Read(read => read.Action("WeeklyRevenue", "Home", new { AdditionalParam = "Test" }))

Or via JavaScript function;

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

function additionalInfo() {
    return {
        AdditionalParam : "test"
    }
}

Or full JavaScript;

transport: {
    read: {
      url: "/Home/WeeklyRevenue",
      type: "POST",
      contentType: "application/json",
      dataType: "json",
      data: {
        AdditionalParam : "Test" 
      }
    }
  }

If you use parameterMap make sure you stringify like following:

parameterMap: function (data, operation) {
                    if (operation != "read") {
                        return kendo.stringify(data.models);
                    }
                    else if (operation === "read") {
                        return kendo.stringify(data);
                    }
                }

In controller something like this

public ActionResult Products_Read([DataSourceRequest] DataSourceRequest request, string AdditionalParam) {...}

Further documentation can be found in here and here.

We can use options given below to pass additional parameters.

//object route values
Read(read => read.Action("vs_Read", "vs", new{id=33})

//js function name
Read(read => read.Action("vs_Read", "vs").Data("passAdParam")

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

In my case : I have a input type text, and I want my grid load data with filter from my text input, and the grid will load data when I press btnSearch:

@(Html.Kendo().Grid<ARM.Models.UserViewModel>()
                .Name("gridUsers")
                .Columns(columns =>
                {
                    columns.Bound(c => c.Code);
                    columns.Bound(c => c.LanID);
                    columns.Bound(c => c.DepartmentName);
                    columns.Bound(c => c.UserRole);
                    columns.Bound(c => c.Level);
                })
                .DataSource(dataSource => dataSource
                    .Ajax()
                    .Read(read => read.Action("GetUserSample", "ApprovalManager").Data("filterLanID"))
                    .PageSize(20)
                )
            )

<script>
function filterLanID() {
        return {
            lanid: $('#txtFilterUserId').val().trim()
        };
    }
function btnSearchOnClick(){
        $('#gridUsers').data("kendoGrid").dataSource.read();
}
</script>

An the controller :

public ActionResult GetUserSample([DataSourceRequest]DataSourceRequest request, string lanid)
        {
            IEnumerable<UserViewModel> userModel = GetListUser(lanid);
            return Json(userModel.ToDataSourceResult(request));
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!