1

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?

1
  • As a solution I am just going to pass the json string to postData and deserialize in the controller method. Couple of extra steps, but it works. Commented May 8, 2012 at 17:58

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply - However, I don't have access to the codebase anymore, so I cannot test your suggestion!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.