Send Additional Parameter in Kendo Grid Read Action

user3210746 picture user3210746 · Apr 22, 2014 · Viewed 39.1k times · Source

I have a kendo Grid as follows.

@(Html.Kendo().Grid<RevenueModel>()
     .Name("WeeklyRevenue")
     .Resizable(resizing => resizing.Columns(true))
     .Columns(columns =>
         {
            columns.Bound(p => p.Number).Width(100);
            columns.Bound(p => p.Type).Width(100);
            columns.Bound(p => p.Week1).Format("{0:c}");
            columns.Bound(p => p.Week2).Format("{0:c}");
            columns.Bound(p => p.Week3).Format("{0:c}");
            columns.Bound(p => p.Week4).Format("{0:c}");
            columns.Bound(p => p.Week5).Format("{0:c}");
            columns.Bound(p => p.TotalRevenue).Format("{0:c}");
         })
     .Scrollable()
     .Events(events => events.Change("onChange").DataBound("onDataBound"))
     .DataSource(dataSource => dataSource.Ajax().Read(read => read.Action("WeeklyRevenue", "Home")).ServerOperation(false))
     .Pageable(pager => pager.Refresh(true))
 )

Here is my Controller code

public ActionResult WeeklyRevenue([DataSourceRequest]DataSourceRequest request)
        {
            ...
            DataSourceResult result = res.ToDataSourceResult(request);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

It works fine. But I want to send additional data when Grid reads data, something like the following;

public ActionResult WeeklyRevenue([DataSourceRequest]DataSourceRequest request, string AdditionalParam)

I couldn't find any solution how to do this. Thanks in advance.

Answer

Atanas Korchev picture Atanas Korchev · Apr 22, 2014

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>