0
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.

2
  • I can access Model.Mydatalist with Model.Mydatalist[1] (a certain Int id) in my javascript, but I need to access Model.Mydatalist[i]. Commented May 27, 2010 at 15:24
  • What is in your Model.Mydatalist? is it a list of strings, ints, objects? Commented May 27, 2010 at 15:45

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I can access Model.Mydatalist with Model.Mydatalist[1] (a certain Int id) in my javascript, but I need to access Model.Mydatalist[i].
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.
I see, I may need to define new action method to return JSON with proper format. Thank you. I cannot vote???
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..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.