title: asp.net mvc display ViewBag dictionary to javascript
body: <p>i'm new in C# asp.net mvc. I'm making a ViewBag that contains Dictionary data type in controller and i want to get and display the value by javascript in .cshtml to code it with googlemaps function.</p>
  
  <p>here's my dictionary &amp; viewbag code from the controller:</p>
  
  <pre><code>Dictionary&lt;string, string&gt; alamatVehicleMarker = new Dictionary&lt;string, string&gt;();
  alamatVehicleMarker.Add("v1","Bali");
  alamatVehicleMarker.Add("v2","Jakarta");
  alamatVehicleMarker.Add("v3","Bandung");
  
  ViewBag.getAlamatVehicle = alamatVehicleMarker;
  </code></pre>
  
  <p>can you help me how to get the ViewBag.getAlamatVehicle in  and how to loop through it please</p>
  
  <p><strong>EDITED</strong></p>
  
  <p>i've tried this :</p>
  
  <pre><code>&lt;script&gt;
  function GetViewBagDictionary(id) {
  
              @foreach (var item in ViewBag.getAlamatVehicle)
              {
                  var key = item.Key;
                  var Value = item.Value;
  
                  if (key == id)
                  {
                      return Value;
                  }
              }
  
              return "not found";
          }
  &lt;script&gt;
  </code></pre>
  
  <p>but inside the if function it's give a error said that : <code>The name 'id' does not exist in the current context</code></p>