asp.net mvc显示ViewBag字典到javascript

时间:2016-08-12 10:10:14

标签: javascript c# asp.net asp.net-mvc asp.net-mvc-4

我是C#asp.net mvc的新手。我正在制作一个包含控制器中字典数据类型的ViewBag,我希望通过.cshtml中的javascript获取并显示该值,以使用googlemaps函数对其进行编码。

这是我的字典&来自控制器的viewbag代码:

Dictionary<string, string> alamatVehicleMarker = new Dictionary<string, string>();
alamatVehicleMarker.Add("v1","Bali");
alamatVehicleMarker.Add("v2","Jakarta");
alamatVehicleMarker.Add("v3","Bandung");

ViewBag.getAlamatVehicle = alamatVehicleMarker;

你可以帮我看看如何获​​取ViewBag.getAlamatVehicle以及如何循环它

EDITED

我试过这个:

<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>

但在if函数中,它会给出一个错误:The name 'id' does not exist in the current context

3 个答案:

答案 0 :(得分:4)

这样的东西可以正常工作小心数据类型,把它放在cshtml中。

var dictionary = @Html.Raw(Json.Encode(ViewBag.getAlamatVehicle));
for(var key in dictionary ){
    var value = dictionary[key];
    console.log(value);
}

资源:

Html Raw

Json Encode

答案 1 :(得分:2)

以下是我如何将VeiwBag转换为javascript的ViewBag对象

<urlrewrite use-context="true">
    <rule>
        <from>^/services/someService$</from>
        <to last="true">/someService.do?%{QUERY_STRING}</to>
    </rule>
</urlrewrite>

结果:

<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>

控制台:

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"
    }
}

以下是如何实现该功能:

Object {v1: "Bali", v2: "Jakarta", v3: "Bandung"}

控制台:

function GetViewBagDictionary(id) {
    if (ViewBag.getAlamatVehicle.hasOwnProperty(id))
        return ViewBag.getAlamatVehicle[id];
    return "not found";
}
console.log(GetViewBagDictionary("v2"));

测试:

Jakarta

控制台:

console.log(GetViewBagDictionary("v4"));

答案 2 :(得分:1)

请在javascript中尝试foreach循环。

         function GetViewBagDictionary() {

            @foreach (var item in ViewBag.getAlamatVehicle)
             {
                 var key = item.Key;
                 var Value = item.Value;
             }


            }

sample screen