0

I want to pass viewbag - keyvaluepair data to javascript code.

I tried code below;

<script type="text/javascript">

@foreach (var jtcontentInfo in (List<KeyValuePair<string, string>>)ViewBag.JumpToContentInfo)
    {
        var someStringValue = @jtcontentInfo.Key;   // It works but I cant read it from javascript.
    }

</script>

What is the best way of handling? Any help will be greatly appreciated.

Nebide Yildiz
  • 4,031
  • 2
  • 15
  • 12
  • What does the generated HTML source look like? – row1 Mar 18 '14 at 14:49
  • I dont want to generate html with result I want to use data for another logic operation. – Nebide Yildiz Mar 18 '14 at 15:04
  • I mean, can you please should a snippet for the generated HTML so that we can see what the result currently looks like. – row1 Mar 18 '14 at 15:08
  • Sorry if I misunderstood, I wont be generate any html with result. I will get data from keyvaluepair list and then use it in some javascript code. I just want to get someStringValue. – Nebide Yildiz Mar 18 '14 at 15:12
  • That's right, but I want to get what currently generated JavaScript looks like so that we can debug the issue. – row1 Mar 18 '14 at 15:15

2 Answers2

0

I think you have two problems here.

  1. You are redeclaring someStringValue each iteration of your loop.
  2. You should probably put the key value inside quotes.

Perhaps you can put the values into an array:

<script type="text/javascript">
var values = [];
@foreach (var jtcontentInfo in (List<KeyValuePair<string, string>>)ViewBag.JumpToContentInfo)
    {
        <text>values.push('@jtcontentInfo.Key');</text>
    }
</script>
row1
  • 5,568
  • 3
  • 46
  • 72
  • Thank you for ur answer but I already tried this way. If I define values as a global variable; first in foreach loop values array is acting like undefined variable sorry for my english but I mean I cant reach values in foreach, second thing application gets a compiler error like below with this kind of definition. – Nebide Yildiz Mar 18 '14 at 14:58
  • Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS1012: Too many characters in character literal Source Error: Line 128: @foreach (var jtcontentInfo in (List>)ViewBag.JumpToContentInfo) Line 129: { Line 130: values.push('@jtcontentInfo.Key'); Line 131: } Line 132: – Nebide Yildiz Mar 18 '14 at 14:59
  • @row11 thank you so much, it is ok now. I can reach values array from js. Kind regards. – Nebide Yildiz Mar 18 '14 at 15:20
  • cool! Instead of any array you could also create an object which had properties with the same names as the keys e.g. `var values = {};` and then something like this in your loop `values['@jtcontentInfo.Key'] = '@jtcontentInfo.Value';` and then use it in your JS like `values.KEYNAME` – row1 Mar 19 '14 at 01:23
0

You can't store directly into a JavaScript variable; you have to remember that the value will merely be printed to the HTML response, and then after the client receives it and IIS has move on to some other task, then the JS will be processed by the browser, and the variable will be set to that value.

So, for example, if @jtcontentInfo.Key was "Foo", the resulting JS would be:

var someStringValue = Foo;

Which would be interpreted as a variable named Foo not the string "Foo". To fix it, you just need to wrap it with quotes:

var someStringValue = "@jtcontentInfo.Key";
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Thank you for ur answer. So I should store it for ex. in a hidden input and then read it from here. I found that way before ask here but I couldnt believe that it is the only and best way for this case. – Nebide Yildiz Mar 18 '14 at 15:02
  • No, it doesn't have to be a hidden input, you just need to pay attention to quotes when you try to set a string value in a JS variable. – Chris Pratt Mar 18 '14 at 15:15