How can I get a JsonResult object as a string so I can modify it?
I am using the FlexiGrid jQuery plug in and I need to get a JSON object back form my MVC App, simple enough if the FlexiGrid took just the object but I need to add a few items to the response string for it to work properly with FlexiGrid.
So here is a portion of my controller code:
If Request.QueryString("json") IsNot Nothing Then Dim data As New StringBuilder() data.Append("page: " & pageIndex & "," & vbCrLf) data.Append("total: " & ViewData.TotalCount & "," & vbCrLf) data.Append("rows: ") data.Append(Json(objCustomerList)) Return Content(data.ToString()) End If
Unfortunately in the above code Json(objCustomerList) returns 'System.Web.MVV.JsonResult' instead of the desired JSON string data. I also tried Json(objCustomerList).ToString() just to see what would happen and the same thing again.
My recommendation would be to take a slightly different approach.
- Then when you pass that FlexiGridModel to Json() it would just work, no need to build a JSON string using StringBuilder.
Some relevant links:
- ASP.NET MVC JsonResult source
- Codeproject: ASP.NET MVC Flexigrid sample
You could also do this:
Simple as that :D
edit: code high lighting
I ended up modifiying the Codeproject example a bit:
Now in my controller I just call:
Return Content(GetFlexiGridJSON(pageIndex, TotalCount, objCustomerList))
As long as the object I pass is a list of objects it works perfectly.
This article describes how to get Flexigrid working with MVC step-by-step: