I have MVC 3 application and I have dictionary of type Dictionary<String,int> in c#. I have pass this dictionary using ViewBag to the Layout view from the Index action. Layout view consist of the jquery code. Here, I want to built array as follows in jquery.
var s1 = [['06/15/2009 16:00:00', 112000], ['06/16/2009 16:00:00', 122000], ['06/17/2009 16:00:00', 104000], ['06/18/2009 16:00:00', 99000], ['06/19/2009 16:00:00', 121000],
['06/20/2009 16:00:00', 148000], ['06/21/2009 16:00:00', 114000], ['06/22/2009 16:00:00', 133000], ['06/22/2009 16:00:00', 161000], ['06/23/2009 16:00:00', 173000]];
How Can I built above jquery array looping through dictionary which passed using viewbag.
First things first there’s no such notion as jquery array. It’s called javascript array. Second why do you wanna loop at all?
Use a JSON serializer to do the job for you:
Oh sorry, I forgot that you are using
ViewBagand not view models (which is what you should be using). In this case you will need to cast whatever you have stuffed into this ViewBag to the corresponding type before being able to do something useful with it (such as a LINQ query for example):