[英]Inserting into a JavaScript object inside razor foreach?
當我在局部視圖中循環瀏覽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>
試試這個: 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.