Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc display ViewBag dictionary to javascript

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

like image 813
Shasapo Avatar asked Mar 12 '23 10:03

Shasapo


2 Answers

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

like image 117
Jonathan Newton Avatar answered Mar 21 '23 11:03

Jonathan Newton


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
like image 37
Aung Myo Linn Avatar answered Mar 21 '23 11:03

Aung Myo Linn