0

我正在研究 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 响应

4

1 回答 1

1

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

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

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

于 2013-10-17T15:23:24.803 回答