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.
here's my dictionary & viewbag code 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;
can you help me how to get the ViewBag.getAlamatVehicle in and how to loop through it please
EDITED
i've tried this :
<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>
but inside the if function it's give a error said that : The name 'id' does not exist in the current context
Something like this would work just be careful of the datatypes, put this in the cshtml.
var dictionary = @Html.Raw(Json.Encode(ViewBag.getAlamatVehicle));
for(var key in dictionary ){
var value = dictionary[key];
console.log(value);
}
Resources:
Html Raw
Json Encode
Here's how I convert VeiwBag into javascript's ViewBag object
<script type="text/javascript">
var ViewBag = {
IsDesigner: ("@ViewBag.IsDesigner").toLowerCase() === 'true',
IsAdmin: ("@ViewBag.IsAdmin").toLowerCase() === 'true',
EmptyGuid: "@ViewBag.EmptyGuid",
Advertisers_ID: "@ViewBag.Advertisers_ID",
Primary_Category: "@ViewBag.Primary_Category",
Primary_Category_ID: "@ViewBag.Primary_Category_ID",
Advertiser_Name: "@ViewBag.Advertiser_Name",
Advertiser_NameMM: "@ViewBag.Advertiser_NameMM",
BookTypeName: "@ViewBag.BookTypeName",
getAlamatVehicle: {
@{
int marker_count = 0;
string markers_str = "";
foreach (var item in ViewBag.getAlamatVehicle)
{
markers_str += "\"" + item.Key + "\": \"" + item.Value + "\"";
marker_count++;
if (marker_count < ViewBag.getAlamatVehicle.Count)
{
markers_str += ", ";
}
}
var html_str = new HtmlString(markers_str);
}
@html_str
}
}
console.log(ViewBag.getAlamatVehicle);
</script>
Result:
var ViewBag = {
IsDesigner: ("False").toLowerCase() === 'true',
IsAdmin: ("True").toLowerCase() === 'true',
EmptyGuid: "00000000-0000-0000-0000-000000000000",
Advertisers_ID: "7e49def2-3887-4149-b419-5f1f51d4ec58",
Primary_Category: "Construction Services",
Primary_Category_ID: "ca9381c7-0379-4a72-80df-270287954164",
Advertiser_Name: "Dolphin Ayeyarwady Pile Construction Co., Ltd.",
Advertiser_NameMM: "",
BookTypeName: "YD",
getAlamatVehicle: {
"v1": "Bali", "v2": "Jakarta", "v3": "Bandung"
}
}
Console:
Object {v1: "Bali", v2: "Jakarta", v3: "Bandung"}
Here's how you can implement the function:
function GetViewBagDictionary(id) {
if (ViewBag.getAlamatVehicle.hasOwnProperty(id))
return ViewBag.getAlamatVehicle[id];
return "not found";
}
console.log(GetViewBagDictionary("v2"));
Console:
Jakarta
Test:
console.log(GetViewBagDictionary("v4"));
Console:
not found
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With