mvcgrid - 如何向我的 MVCGrid.Net RetrieveDataMethod 发送附加参数?

标签 mvcgrid mvcgrid.net

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

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

.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
    };
})

这是查看代码:

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

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

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

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

这在技术上是可行的,但 id 现在在 url 中出现了两次: /Reports/Index/21?projectid=21

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

最佳答案

最近添加了一项名为“页面参数”的新功能,可以完全按照您的要求进行操作。确保您使用的是最新的 NuGet 包。

使用方法如下:

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

.WithPageParameterNames("ProjectId")

2) 从 View 中传入参数的值

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

3) 使用 RetrieveDataMethod 中的值

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

关于mvcgrid - 如何向我的 MVCGrid.Net RetrieveDataMethod 发送附加参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30044391/

相关文章:

html - web Grid 列传递样式类

c# - MVC Identity 2.0 和管理角色

javascript - 在 GridMVC 中创建自定义时间小部件

c# - 如何在 Asp.Net mvc 5 的 grid.mvc 中添加两个按钮

css - MVCGrid.net - 将列标题和排序图标保持在一行中

javascript - MVC 5 中的搜索参数

asp.net-mvc - MVCGrid.Net 排序问题 - sortdirection