【问题标题】:How do i send an additional parameter to my MVCGrid.Net RetrieveDataMethod?如何向我的 MVCGrid.Net RetrieveDataMethod 发送附加参数?
【发布时间】:2015-07-14 16:20:02
【问题描述】:

我正在使用 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
    };
})

这是查看代码:

<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

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

【问题讨论】:

    标签: mvcgrid mvcgrid.net


    【解决方案1】:

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

    使用方法如下:

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

    .WithPageParameterNames("ProjectId")

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

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

    3) 使用 RetrieveDataMethod 中的值

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

    【讨论】:

    • 另外我只想说我一直在寻找 Telerik 的替代品,这可能是目前最好的选择。很棒的工作。
    • 谢谢!我很感激。
    猜你喜欢
    • 2011-03-11
    • 2017-08-25
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 2017-09-19
    • 2016-10-21
    • 1970-01-01
    • 2015-10-05
    相关资源
    最近更新 更多