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
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" %>
JSON Test
Returned JSON:
{ "Dict": { "Sep 05, 2010": [ { "Name": "event 1", "Id": 1 },
{ "Name": "event 2", "Id": 2 } ],
"Sep 07, 2010": [ { "Name": "event 3", "Id": 3 } ] } }