mvcgridmvcgrid.net

How do i send an additional parameter to my MVCGrid.Net RetrieveDataMethod?


I'm using MVCGrid.net (http://mvcgrid.net). First of all, fantastic grid tool! I have my grid working properly, however when i'm populating my grid I need to pass an additional parameter to my RetrieveDataMethod. The value i need to pass is part of my view model and I'm pretty sure I need to pass it as an AdditionalQueryOption. I don't want this to be a filter because i don't want it only sent after a client side event. I want it passed to my RetrieveDataMethod always. i tried adding data-mvcgrid-type='additionalQueryOption' to my hidden input, and it still wasn't sent. Then i noticed that this requires data-mvcgrid-apply-additional='event' to trigger the event. how is this different than a filter? Is there a grid loading event that i can hook into to register my hidden value as an additional query option? What's the right approach for my situation here?

Here's my Retrieve Data Method definition for my grid:

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

This is the View Code:

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

The additional option i need is passed as a query string parameter. so i tried setting the additional query options on document ready.

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

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

That technically works, but the id is in the url twice now: /Reports/Index/21?projectid=21

i can live with this, but is there a better approach here?


Solution

  • A new feature was added recently called Page Parameters to do exactly what you are trying to do. Make sure you are using the latest NuGet package.

    Here's how to use it:

    1) Add this to your grid config:

    .WithPageParameterNames("ProjectId")

    2) Pass in the value of the parameter from the view

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

    3) use the value in your RetrieveDataMethod

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