你可以将C#字典为Javascript关联数组使用asp.net mvc的JSON() [英] Can you convert C# dictionary to Javascript associative array using asp.net mvc Json()
问题描述
我最近<一个href=\"http://stackoverflow.com/questions/3643430/how-can-i-send-a-dictionary-from-c-to-javascript-using-json\">asked这个问题,但经过一些反应和一些研究,我想改变什么,我其实是问。
我看到一个<一个href=\"http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx\">number大约从JavaScript发送关联数组到C#控制器操作的博客文章,但我想的正好相反。我想JSON返回到客户端的字典(从我的研究词典的JavaScript相当于是一个关联数组)。
当我把一个普通的字典在C锐利和调用JSON()就可以了,并尝试将其返回到JavaScript中,它只是吹了,我无法甚至把一个断点在JavaScript端。例如:
C#code:
词典&LT;字符串列表&LT; CalendarEvent&GT;&GT;字典= events.GroupBy(R = GT; r.Date.ToString(MMM DD,YYYY))ToDictionary。(组=&GT; group.Key,组= GT; group.ToList()); 返回JSON(新
{
快译通=字典
}
});
的Javascript code:
$。员额('/ myController的/刷新,功能(数据){ VAR calendarDictionary = data.Dict; }JSON);
您可能本来可以更具体一点有关的它只是炸毁的一部分,但这里是为我工作得很好的例子:
型号:
公共类CalendarEvent
{
公共字符串名称{;组; }
公众的DateTime日期{搞定;组; }
公众诠释标识{搞定;组; }
}
控制器:
公共类HomeController的:控制器
{
公众的ActionResult指数()
{
返回查看();
} [HttpPost]
公众的ActionResult刷新()
{
VAR模型=新[]
{
新CalendarEvent
{
ID = 1,
NAME =事件1
日期= DateTime.Now
},
新CalendarEvent
{
ID = 2,
NAME =事件2
日期= DateTime.Now
},
新CalendarEvent
{
ID = 3,
NAME =事件3
日期= DateTime.Now.AddDays(2)
},
}
.ToList()
.ConvertAll(A =&gt;新建
{
一个名字,
援助,
日期= a.Date.ToString(MMM DD,YYYY),
})
.GroupBy(R =&GT; r.Date)
.ToDictionary(
组=&GT; group.Key,
组=&GT; group.Select(X =&gt;新建{x.Name,x.Id})
);
返回JSON(新{快译通=模型});
}
}
查看:
&LT;%@页面语言=C#继承=System.Web.Mvc.ViewPage%GT;
&LT;!DOCTYPE HTML&GT;
&LT; HTML和GT;
&LT; HEAD&GT;
&LT; META HTTP-EQUIV =Content-Type的CONTENT =text / html的;字符集= UTF-8/&GT;
&LT;标题&GT; JSON测试与LT; /标题&GT;
&LT;脚本类型=文/ JavaScript的SRC =http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js&GT;&LT; / SCRIPT&GT;
&LT;脚本类型=文/ JavaScript的&GT;
$(函数(){
.post的$('/家庭/刷新'功能(数据){
// TODO:在此处操纵data.Dict
},'JSON');
});
&LT; / SCRIPT&GT;
&LT; /头&GT;
&LT;身体GT;&LT; /身体GT;
&LT; / HTML&GT;
返回的JSON:
{快译通:{2010年9月5日:[{姓名:事件1,标识:1},
{姓名:事件2,标识:2}],
2010年9月7日:[{姓名:事件3,ID:3}]}}
I recently asked this question, but after some of the responses and some research, i wanted to change what i was actually asking.
i have seen a number of blog posts about sending associative arrays from javascript to C# controller action but i want the opposite. I want to return json to a client as a dictionary (from my research the javascript equivalent of dictionary is an associative array).
when i take a regular dictionary in c sharp and call Json() on it and try to return it to javascript, it just blows up and i am unable to even put a breakpoint on the javascript side. For example:
C# Code:
Dictionary<string, List<CalendarEvent>> dict = events.GroupBy(r => r.Date.ToString("MMM dd, yyyy")).ToDictionary(group => group.Key, group => group.ToList());
return Json(new
{
Dict = dict
}
});
Javascript Code:
$.post('/MyController/Refresh', function (data) {
var calendarDictionary = data.Dict;
}, "json");
You probably could have been a little more specific about the it just blows up part but here's an example that works fine for me:
Model:
public class CalendarEvent
{
public string Name { get; set; }
public DateTime Date { get; set; }
public int Id { get; set; }
}
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Refresh()
{
var model = new[]
{
new CalendarEvent
{
Id = 1,
Name = "event 1",
Date = DateTime.Now
},
new CalendarEvent
{
Id = 2,
Name = "event 2",
Date = DateTime.Now
},
new CalendarEvent
{
Id = 3,
Name = "event 3",
Date = DateTime.Now.AddDays(2)
},
}
.ToList()
.ConvertAll(a => new
{
a.Name,
a.Id,
Date = a.Date.ToString("MMM dd, yyyy"),
})
.GroupBy(r => r.Date)
.ToDictionary(
group => group.Key,
group => group.Select(x => new { x.Name, x.Id })
);
return Json(new { Dict = model });
}
}
View:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JSON Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$.post('/home/refresh', function(data) {
// TODO : manipulate the data.Dict here
}, 'json');
});
</script>
</head>
<body>
</body>
</html>
Returned JSON:
{ "Dict": { "Sep 05, 2010": [ { "Name": "event 1", "Id": 1 },
{ "Name": "event 2", "Id": 2 } ],
"Sep 07, 2010": [ { "Name": "event 3", "Id": 3 } ] } }
这篇关于你可以将C#字典为Javascript关联数组使用asp.net mvc的JSON()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!