Вы, возможно, могли бы быть немного более конкретным в отношении , который просто взрывает часть, но вот пример, который отлично работает для меня:
Модель:
public class CalendarEvent
{
public string Name { get; set; }
public DateTime Date { get; set; }
public int Id { get; set; }
}
Контроллер:
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 });
}
}
Просмотр:
<%@ 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>
Возвращено JSON:
{ "Dict": { "Sep 05, 2010": [ { "Name": "event 1", "Id": 1 },
{ "Name": "event 2", "Id": 2 } ],
"Sep 07, 2010": [ { "Name": "event 3", "Id": 3 } ] } }