asp.net mvc - mvc pass viewbag keyvaluepair parameter to javascript code? -


i want pass viewbag - keyvaluepair data javascript code.

i tried code below;

<script type="text/javascript">  @foreach (var jtcontentinfo in (list<keyvaluepair<string, string>>)viewbag.jumptocontentinfo)     {         var somestringvalue = @jtcontentinfo.key;   // works cant read javascript.     }  </script> 

what best way of handling? appreciated.

i think have 2 problems here.

  1. you redeclaring somestringvalue each iteration of loop.
  2. you should put key value inside quotes.

perhaps can put values array:

<script type="text/javascript"> var values = []; @foreach (var jtcontentinfo in (list<keyvaluepair<string, string>>)viewbag.jumptocontentinfo)     {         <text>values.push('@jtcontentinfo.key');</text>     } </script> 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -