You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# MVC应用中Kendo Grid如何向控制器传递多个参数

给Kendo Grid Ajax数据源添加第二个参数的两种方法

方法1:直接在Data的JSON对象中扩展参数

直接在原来的参数对象里加上第二个参数即可,比如假设第二个参数是从Model获取的AnotherParam:

@(Html.Kendo().Grid<OwnerViewModel>()
    .Name("OwnerGrid")
    .AutoBind(true)
    .Columns(columns =>
    {
        columns.Bound(pl => pl.RecordDate).Width(20).Title("Record Date").Format("{0:MM/dd/yyyy}");
        columns.Bound(pl => pl.Unit).Width(15).Title("Unit");
        columns.Bound(pl => pl.Own_Name).Width(45).Title("Name");
        columns.Bound(pl => pl.Own_Salutation).Width(45).Title("Salutation");
        columns.Bound(pl => pl.Own_MailAddr).Width(45).Title("Mailing Addr");
        columns.Bound(pl => pl.Own_City_St_Zip).Width(45).Title("City,State,Zip");
        columns.Bound(pl => pl.Own_PropAddr).Width(45).Title("Property Address");
    })
    .Pageable()
    .Sortable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(5)
        // 扩展参数对象,添加第二个参数
        .Read(read => { read.Action("GetOwnerList", "Owners").Data("{ FileNum: " + @Model.FileNum + ", AnotherParam: " + @Model.AnotherParam + "}"); })
        )
    )

方法2:用JavaScript函数返回参数对象(更灵活)

如果参数需要动态获取或有复杂逻辑,推荐用这种方式:

  1. 先在页面添加一个JS函数,返回包含两个参数的对象:
<script>
function getGridParams() {
    // 可根据需求动态获取参数值,比如从页面元素或Model中取
    return {
        FileNum: @Model.FileNum,
        AnotherParam: "@Model.AnotherParam" // 字符串类型参数记得加引号
    };
}
</script>
  1. 修改Grid的DataSource配置,调用这个函数:
@(Html.Kendo().Grid<OwnerViewModel>()
    .Name("OwnerGrid")
    .AutoBind(true)
    .Columns(columns =>
    {
        columns.Bound(pl => pl.RecordDate).Width(20).Title("Record Date").Format("{0:MM/dd/yyyy}");
        columns.Bound(pl => pl.Unit).Width(15).Title("Unit");
        columns.Bound(pl => pl.Own_Name).Width(45).Title("Name");
        columns.Bound(pl => pl.Own_Salutation).Width(45).Title("Salutation");
        columns.Bound(pl => pl.Own_MailAddr).Width(45).Title("Mailing Addr");
        columns.Bound(pl => pl.Own_City_St_Zip).Width(45).Title("City,State,Zip");
        columns.Bound(pl => pl.Own_PropAddr).Width(45).Title("Property Address");
    })
    .Pageable()
    .Sortable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(5)
        // 传入函数名,Kendo会自动调用它获取参数
        .Read(read => { read.Action("GetOwnerList", "Owners").Data("getGridParams"); })
        )
    )

配套修改控制器方法

确保Owners控制器的GetOwnerList方法能接收这两个参数:

public ActionResult GetOwnerList(int FileNum, int AnotherParam, [DataSourceRequest] DataSourceRequest request)
{
    // 这里写你的业务逻辑,根据两个参数获取数据
    var ownerData = // 数据获取逻辑
    return Json(ownerData.ToDataSourceResult(request));
}

内容的提问来源于stack exchange,提问作者Jonathan Small

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 13:47:56