This project is read-only.

Returning JsonArray from MVC Action

May 6, 2015 at 6:10 PM
Edited May 6, 2015 at 6:17 PM
In Json Toolkit v 4.3.833, I'm building an array of JsonObject like this:
        JsonArray joArray = new JsonArray();
        foreach (var row in lstRows)
          JsonObject jrow = new JsonObject();
          foreach (var cell in row.RowAttributes)
            string id = cell.ListColumns.ColumnName.Replace(" ", "");
            if (id == "Item#")
              id = "id";
            if (id != "id")
              string val = cell.CellValue.Replace("\"", "\\\"");
              jrow.Add(id, val);
              int iVal = Int32.Parse(cell.CellValue); //e.g. 1
              jrow.Add(id, iVal);
          jrow.Add("cmdButton", "Edit");
This gives me the SlickGrid table data i need to return.
If I try to return this as JsonResult from my MVC Action, the Ajax callback sees the array element 0 as Key: "id", Value: 1 instead of { "id": 1 }.
I'm using this line to return the JsonResult data: return Json(joArray, JsonRequestBehavior.AllowGet).

As a workaround, I changed the Action return value to "string" and return joArray.ToString() instead. This works in my Ajax callback, but I was wondering if it is the best way of doing this or is there a better way?