I am trying to use C# in javascript like we are using it in MVC Razor View using @ sign,
like suppose an array name list is passed to the View so we can access it in View like:
View
Length of array : <input type="text" value="@Model.list.Length" />
Or we can iterate list array also like:
@for(int i=0; i< Model.list.Length; i++)
{
console.log(Model.list[i]);
}
But my question is how we can iterate or use this array in the javascript code , something similar to :
JS
for(var i=0; i<@Model.list.Length; i++)
{
$("body").append("<h1></h1>").html(@Model.list[i]);
}
Thanks !
C#commands into native js. This is pretty easy for plain values, and a lot trickier for complex objects. If your preprocessor is capable to convert aC#-Object into aJSobject, then you can do this. If not, you can't because you won't be able to access the values of the array.