如何向MVCGrid.Net RetrieveDataMethod发送其他参数?

Don*_*uts 3 mvcgrid mvcgrid.net

我正在使用MVCGrid.net(http://mvcgrid.net).首先,神奇的网格工具!我的网格正常工作,但是当我填充网格时,我需要将另一个参数传递给我的RetrieveDataMethod.我需要传递的值是我的视图模型的一部分,我很确定我需要将它作为AdditionalQueryOption传递.我不希望这是一个过滤器,因为我不希望它只在客户端事件后发送.我希望它总是传递给我的RetrieveDataMethod.我尝试将data-mvcgrid-type ='additionalQueryOption'添加到我的隐藏输入中,但仍未发送.然后我注意到这需要data-mvcgrid-apply-additional ='event'来触发事件.这与过滤器有什么不同?是否有一个网格加载事件,我可以挂钩注册我的隐藏值作为一个额外的查询选项?对我的情况有什么正确的解决方法?

这是我的网格的检索数据方法定义:

.WithRetrieveDataMethod((context) =>
{
    var options = context.QueryOptions;
// this is the bit i need
    var projectId = ???;
    options.AdditionalQueryOptions.Add("projectId", projectId);

    int totalRecords;
    var items = ReportManager.GetReports(out totalRecords, options);

    return new QueryResult<ReportSummaryViewModel>()
    {
        Items = items,
        TotalRecords = totalRecords
    };
})
Run Code Online (Sandbox Code Playgroud)

这是查看代码:

<h1>Reports for @Model.ProjectName</h1>

<p class="form-inline">
    <button type="button" class="btn btn-default" onclick="window.location.href='@Url.Action("Index", "Home")'">Back</button>
    <button type="button" class="btn btn-primary" onclick="window.location.href='@Url.Action("Create", "Reports", new { @id = Model.ProjectId })'">New Report</button>
</p>

<!-- This is the hidden input i'd like to pass as an additional query option-->
@Html.HiddenFor(x => x.ProjectId)

<div id="reportList">
    @Html.MVCGrid("ReportsGrid")
</div>
Run Code Online (Sandbox Code Playgroud)

我需要的附加选项作为查询字符串参数传递.所以我尝试在文档就绪上设置其他查询选项.

$(function () {
    initAdditionalOptions();
});

function initAdditionalOptions() {
    MVCGrid.setAdditionalQueryOptions("ReportsGrid", { projectId: $('#ProjectId').val() });
}
Run Code Online (Sandbox Code Playgroud)

这在技术上有效,但id现在在url中两次:/ Reports/Index/21?projectid = 21

我可以忍受这个,但这里有更好的方法吗?

Big*_*714 7

最近添加了一个名为Page Parameters的新功能,可以完全按照您的要求进行操作.确保您使用的是最新的NuGet包.

以下是如何使用它:

1)将其添加到您的网格配置:

.WithPageParameterNames("ProjectId")

2)从视图中传入参数的值

@Html.MVCGrid("PPGrid", new { ProjectId = "whatever" })

3)使用RetrieveDataMethod中的值

string projectIdVal = context.QueryOptions.GetPageParameterString("ProjectId");