views:

130

answers:

1
for(var i=0; i<<%=Model.Mydatalist.Count%>;i++)
{
//then I need to pass i to Mydatalist like Model.Mydatalist[i]

}

//var myJSdata="<%Model.Mydatalist["&i&"]%>"; is not working.. as system see i (i's value) as a string, and will throw exception: cannot convert string to int.

+1  A: 

You'll need to do some kind of callback to the server for that. Once the page is loaded on the browser and the script runs it is no longer running on the server so you wont be able to access your Model.Mydatalist.

Justin
I can access Model.Mydatalist with Model.Mydatalist[1] (a certain Int id) in my javascript, but I need to access Model.Mydatalist[i].
Bolu
Yes, you will be able to get to `Model.Mydatalist[1]` since that is replaced with whatever is in that index when the page is built, and before it is sent over the wire.<br/>Trying to iterate through it in javascript, in a loop, will happen when the browser renders the page, after it has been sent across the wire.
Justin
I see, I may need to define new action method to return JSON with proper format. Thank you.I cannot vote???
Bolu
np :)JSON is a probably the best way to go, and with ASP MVC it's really easy to write up a quick action to return what you need..
Justin