如何从ASP.net控制器方法获得扩展的json响应

时间:2013-10-17 15:10:38

标签: asp.net-mvc json jqgrid

我正在开发ASP.net MVC Applciation

我在准备从控制器操作方法返回的json响应方面遇到了困难。

我需要这样的事情:

{
    "cityMap": {"11": "Chennai", "12": "Mumbai", "13": "Delhi"},
    "rows": [
        { "SID": "1",  "SNAME": "ABC", "CITY": "11" },
        { "SID": "2",  "SNAME": "XYZ", "CITY": "12" },
        { "SID": "3",  "SNAME": "ACX", "CITY": "13" },
        { "SID": "4",  "SNAME": "KHG", "CITY": "13" },
        { "SID": "5",  "SNAME": "ADF", "CITY": "12" },
        { "SID": "6",  "SNAME": "KKR", "CITY": "11" }
    ]
}

我在sortedlist名称slLocations

中有cityMap值

我有列表liStudents

形式的行值

我使用以下语法发送json响应:

return JSON(new { rows=liStudents},jsonRequestBehaviour.AllowGet)

通过使用上面的语法,我得到了像这样的json响应:

 {
  "rows": [
            { "SID": "1",  "SNAME": "ABC", "CITY": "11" },
            { "SID": "2",  "SNAME": "XYZ", "CITY": "12" },
            { "SID": "3",  "SNAME": "ACX", "CITY": "13" },
            { "SID": "4",  "SNAME": "KHG", "CITY": "13" },
            { "SID": "5",  "SNAME": "ADF", "CITY": "12" },
            { "SID": "6",  "SNAME": "KKR", "CITY": "11" }
        ]
  }

请使用liStudents和liLocations

帮助解决如何将json响应扩展到上面的问题

1 个答案:

答案 0 :(得分:1)

只需将所需的cityMap属性添加到要传递给视图的匿名对象:

var model = new
{
    cityMap = slLocations,
    rows = liStudents,
}
return JSON(model, JsonRequestBehaviour.AllowGet);

这显然假定slLocations变量是SortedList<string, string>