CodeManiac
CodeManiac

Reputation: 1014

Build an array in jquery using loop in dictionary

I want to pass array value for creating graph of jplot function. In Layout cshtml it consist of jquery function for creating the graph and it have an array in the form of:

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]];

I have passed dictionary value to this layout using viewbag and use following code to loop through dictionary.

  @foreach (KeyValuePair<String, int> kvp in ViewBag.dateRange)
                    {  
                        @kvp.Key
                       @kvp.Value          
                    }

How can I built above array for jquery function using razor engine. Dictionary have string and int where string is date value and int is total number value.

Upvotes: 0

Views: 901

Answers (1)

ZafarYousafi
ZafarYousafi

Reputation: 10880

try this

@{
    var dic = ViewBag.dateRange as Dictionary<String,int>;
}

var s1 = [@(String.Join(",", dic.Select(d => String.Format("['{0}',{1}]", d.Key, d.Value)));)];

Upvotes: 1

Related Questions