在Kendo Grid Read Action中发送附加参数

时间:2014-04-22 14:46:19

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

我有一个kendo Grid如下。

@(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))
 )

这是我的控制器代码

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

工作正常。但是我想在Grid读取数据时发送更多数据,如下所示;

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

我找不到任何解决方法如何做到这一点。提前谢谢。

4 个答案:

答案 0 :(得分:21)

如果在服务器端知道附加数据,则应使用接受路由值的Action方法的重载:

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

如果仅在客户端知道此附加数据,则应使用Data方法:

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

答案 1 :(得分:16)

你可以试试这个;

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

或通过JavaScript功能;

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

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

或完整的JavaScript;

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

如果使用parameterMap,请确保按字符串化方式如下:

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

在像这样的控制器中

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

可以在herehere找到更多文档。

答案 2 :(得分:5)

我们可以使用下面给出的选项来传递其他参数。

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

答案 3 :(得分:1)

在我的情况下:我有一个输入类型文本,我希望我的网格加载数据带有来自我的文本输入的过滤器,当我按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>

控制器:

public ActionResult GetUserSample([DataSourceRequest]DataSourceRequest request, string lanid)
        {
            IEnumerable<UserViewModel> userModel = GetListUser(lanid);
            return Json(userModel.ToDataSourceResult(request));
        }