我是C#asp.net mvc的新手。我正在制作一个包含控制器中的Dictionary数据类型的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以及如何遍历它吗?
已编辑
我已经试过了:
<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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句