Recently, I started learning C# asp.net mvc and decided to create a ViewBag containing Dictionary data type in the controller. My goal is to retrieve and display the values using JavaScript in .cshtml for implementing Google Maps functionality.
Here is the code snippet of my dictionary and ViewBag from the controller:
Dictionary<string, string> alamatVehicleMarker = new Dictionary<string, string>();
alamatVehicleMarker.Add("v1","Bali");
alamatVehicleMarker.Add("v2","Jakarta");
alamatVehicleMarker.Add("v3","Bandung");
ViewBag.getAlamatVehicle = alamatVehicleMarker;
I need assistance in fetching the ViewBag.getAlamatVehicle in .cshtml file and also guidance on how to loop through it.
UPDATE
I attempted the following:
<script>
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";
}
<script>
However, an error occurs inside the if function stating:
The name 'id' does not exist in the current context