Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can't access j inside for loop in this line

C#
var points = @Html.Raw(Json.Encode(@Model.objDllCollection[j].Parameters)) 


the error which i'm getting is

The name j does not exist in the current context


JavaScript
function onAdditional() {
        var j;
        for (j = 0; j < @Model.objDllCollection.Count(); j++) {
            debugger
            var ParamArray = new Array();
            var points = @Html.Raw(Json.Encode(@Model.objDllCollection[j].Parameters))
            ParamArray.push(points);

          
        }

    }
Posted
Updated 16-Mar-16 18:09pm
v6

1 solution

The code after "@" is executed server-side before the resulting html is sent to the browser. "j" exists as a javascript variable when the html runs in the browser, so you can't use it in your server-side code as it doesn't exist in that context. You need to construct all of the js using server code, something like this

function onAdditional() {
    debugger;
    var ParamArray = new Array();
    var points;

    @foreach(var m in Model.objDllCollection)
    {
        string p = string.Format("points = {0};\r\nParamArray.push(points);\r\n", Json.Encode(m.Parameters));
        @Html.Raw(p);
    } 
}
 
Share this answer
 
Comments
Sathish km 17-Mar-16 5:47am    
string is not accepting as valid datatype.
How to declare collection model into @Html.Raw(Json.Encode())

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900