views:

25

answers:

2

Im sending a list from my controller class to my View which contain a list of values of type long. Now i want to traverse on that list in jquery to be able to view these values and perform my next operation. Im using Asp.net mvc and jquery. how to convert it so i can access each value?

A: 

I don't use asp.net-mvc, but I can point you in the right direction: basically, you want to serialize your list as a JSON object in your view. You'll either embed the JSON directly in a script block as part of a larger html page, or you'll return the JSON directly as the response to an ajax call. Either way, the point is that you're returning the javascript (JSON) to the browser, where it gets interpreted, and turned into a javascript object (or array) that can then be accessed with jQuery just like any other js object/array.

I think this article shows how to do the AJAX route, which is probably the recommended approach (not knowing any details about the context of your question).

good luck!

Lee
+1  A: 

You could use the JavaScriptSerializer class to turn the model property which is an array into a javascript array:

var values = <%= new JavaScriptSerializer().Serialize(Model.SomeArrayProperty) %>;
// Now use values as a javascript array
Darin Dimitrov