我正在开发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
我有列表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响应扩展到上面的问题答案 0 :(得分:1)
只需将所需的cityMap
属性添加到要传递给视图的匿名对象:
var model = new
{
cityMap = slLocations,
rows = liStudents,
}
return JSON(model, JsonRequestBehaviour.AllowGet);
这显然假定slLocations
变量是SortedList<string, string>
。