Nate Pet
Nate Pet

Reputation: 46322

jqgrid passing parameter to method

I have the following example:

      var url = '@Url.Action("GetProgData", "Prog")' + '?lbId=' + labId;        

      $("#loginList").jqGrid({
       url: url,
       datatype: "json",
       colNames: ['PNum', 'Client', 'Salesperson', 'Email'],
       colModel: [ ...
       ......

The method looks like the following in c# MVC:

     public JsonResult GetLoginData(int rows, int page, string sidx, string sord, string searchField, string searchString, string searchOper, int? labId)

What I like to do is to pass the value of labId conditionally so I have the following but doesn't seem to pass labId as it return null:

       $("#LabId").change(function () {
        labId = $("#LabId").val();
        setupGrid(labId);
        $("#loginList").trigger("reloadGrid", [{ page: 1}]);
       });

Not sure when I do it in the .change of a dropdown the value of labID does't go through fine

Upvotes: 0

Views: 2153

Answers (1)

Oleg
Oleg

Reputation: 222017

I think that you can sent null as the value of labId if

$("#loginList").jqGrid({
    url: '@Url.Action("GetProgData", "Prog")',
    postData: {
        lId: function () {
            var labId = $("#LabId").val();
            return labId === "" ? null : labId;
        }
    },
    datatype: "json",
    colNames: ['PNum', 'Client', 'Salesperson', 'Email',
    ...
});

If the above solution will not work, than you can do the following alternatively

$("#loginList").jqGrid({
    url: '@Url.Action("GetProgData", "Prog")',
    serializeGridData: function (data) {
        var labId = $("#LabId").val();
        return labId === "" ? data : $.extend(true, {}, data, {lId: labId});
    },
    datatype: "json",
    colNames: ['PNum', 'Client', 'Salesperson', 'Email',
    ...
});

Upvotes: 2

Related Questions