当我在局部视图中循环浏览C#列表时,我需要添加到JavaScript对象,我尝试了<text></text>
和@:
,但它们只是呈现纯JavaScript文本,这是完整的局部-查看:
@using Resources;
<script>
var resources = {};
@{
Dictionary<string, string> resx = new Dictionary<string, string>();
foreach (var item in typeof(AppResources).GetProperties().Where(p=>p.PropertyType==typeof(string)))
{
resx.Add(item.Name, item.GetValue(item).ToString());
var key = item.Name;
var value = item.GetValue(item).ToString();
@:resources[@key] = @value;
}
}
console.log("resources_BEGIN");
console.log(resources);
console.log("resources_END");
</script>
这正在HTML源代码中呈现:
<script>
var resources = {};
resources[lbAccountSettings] = Account settings;
resources[lbActivate] = Activate;
resources[lbArabicName] = Arabic Name;
resources[lbAreas] = Areas;
console.log("resources_BEGIN");
console.log(resources);
console.log("resources_END");
</script>
答案 0 :(得分:0)
尝试以下操作:var jsvariable=@Html.Raw(Json.Encode(<your_charp_variable>))
已编辑:
<script>
var resources = {}
@{
Dictionary<string, string> resx = new Dictionary<string, string>();
foreach (var item in typeof(AppResource).GetProperties().Where(p=>p.PropertyType==typeof(string)))
{
resx.Add(item.Key, item.Value);
}
}
resources=@Html.Raw(Json.Encode(resx));
</script>