rgwozdz
rgwozdz

Reputation: 1143

jqGrid: how to pass complex javascript object as postData to ASP MVC controller method

I need to pass a JS object to my controller method jqGrid. The object is call "activeFilters" - here is the object represented as JSON:

{"family":
     [{
      "filterDisplayName":"Performance Status",
      "filterDbName":"CurrentStatus",
      "filterValueList":"On Plan"
      }]
 }

Having problems passing the above to my jqGrid (details further down). But I can pass the object to a controller with Ajax very simply:

$.ajax({
    url: myMethodPath,
    type: 'POST',
    dataType: 'html',
    data: JSON.stringify(activeFilters),
    contentType: 'application/json; charset=utf-8',
    success: function (result) {
        alert("success")
    },
    error: function () {
        alert("Error:");
    }
});

My test controller method looks like:

    [HttpPost]
    public ActionResult DataTest(JsonFilterFamily activeFilters)
    {
        return PartialView();
    }

Add the structure of JsonFilterFamily looks like:

public class JsonFilterFamily
{
    public List<FilterFamilyMember> family { get; set; }
} 

public class FilterFamilyMember
{
    public string filterDisplayName { get; set; }
    public string filterDbName { get; set; }
    public string filterValueList { get; set; }
}

With the above Ajax, the JS object gets sent to the controller without problems. But I just can't figure out how to send the same JS object as the postData in a call to jqGrid controller. I must have the syntax wrong for post data. Here is what I am using:

$("#myJqGrid").setGridParam({ postData: { family: activeFilters.family} }).trigger("reloadGrid");

But when the controller fires, something strange happens. The debugger tells me the count of family = 1; but filterDisplayName, filterDbName, filterValueList are null. Any ideas on what is wrong?

Upvotes: 1

Views: 1732

Answers (1)

Soumen Banerjee
Soumen Banerjee

Reputation: 45

In your controller method, try putting:

var filterFamilyMember = new FilterFamilyMember();
TryUpdateModel(filterFamilyMember);

TryUpdateModel is a MVC controller method and this will put the data in your object.

Upvotes: 1

Related Questions