简体   繁体   English

模型中的Javascript使用清单

[英]Javascript use list from model

Can somebody help me this is my code: 有人可以帮我这是我的代码:

for(var i = 0 ; i < @Model.BegrotingsPosten.Count; i++){
    var percentage = '@Html.Raw(Json.Encode(Model.BegrotingsPosten[i].BelastingsPercentage))';
    var berekening = (valueBelastingen / 100) * percentage;
    var afgerond = (Math.round(berekening * 100) / 100);
    $('.euro').eq(i).text(afgerond);
}

The @Html.Raw(Json.Encode(Model.BegrotingsPost[i].BelastingsPercentage)); @Html.Raw(Json.Encode(Model.BegrotingsPost[i].BelastingsPercentage)); does not work and generates the following error 不起作用并生成以下错误

The name i does not exist in the current context`. 名称i在当前上下文中不存在。

If I use @Html.Raw(Json.Encode(Model.BegrotingsPost[1].BelastingsPercentage)); 如果我使用@Html.Raw(Json.Encode(Model.BegrotingsPost[1].BelastingsPercentage)); it does work, but I have to loop 它确实有效,但我必须循环

@Html.Raw() is razor code and is evaluated on the server before its been sent to the view. @Html.Raw()是剃刀代码,在将其发送到视图之前在服务器上进行评估。 You i is a javascript variable which does not even exist at that point. i是一个JavaScript变量,该变量甚至在那时还不存在。

You can store the whole collection in a javascript array, and then loop through it in the script 您可以将整个集合存储在javascript数组中,然后在脚本中循环遍历

var data = @Html.Raw(Json.Encode(Model.BegrotingsPosten));
for (var i = 0 ; i < data.length; i++) {
    var percentage = data[i].BelastingsPercentage;
    ....
}

Note, If your only using the one property of your collection, you may want to consider a linq .Select() to return just a collection of that properties values. 注意,如果仅使用集合的一个属性,则可能需要考虑使用linq .Select()仅返回该属性值的集合。

Try splitting out the usage of i to another var, so change 尝试将i的用法拆分为另一个变量,因此请更改

var percentage = '@Html.Raw(Json.Encode(Model.BegrotingsPosten[i].BelastingsPercentage))';

to

var percent = @Model.BegrotingsPosten[i].BelastingsPercentage;
var percentage = '@Html.Raw(Json.Encode(percentage))';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM