Can you convert C# dictionary to Javascript associative array using asp.net mvc Json()

Viewed 19414

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");
4 Answers
Related