asp.net mvc - Trying to pass Array from Server Side to be read from Javascript in my MVC app -


any appreciated, not sure why not working, in server side code have:

foreach (var item in model)                {                    if (item.sitelocation == null || item.sitelocation.sitelocationid == 0)                    { }                    else                    {                         if ((item.sitelocation.latitude != 0) && (item.sitelocation.longitude != 0))                            page.clientscript.registerarraydeclaration("sites", "'" + item.siteid + "','" + item.sitedescription + "','" + item.sitelocation.longitude + "','" + item.sitelocation.latitude + "'");                    } 

...........then try reference array using following code in javascript :

    (var = 0; < sites.length; i++) {         // create , element object of type "option"         alert(sites[i]);     } 

..........but says "sites undefined"

i've debugged server side , "page.clientscript.registerarraydeclaration" line runs few times no idea why object not there when use javascript, ideas?

  1. page.clientscript.registerarraydeclaration?
  2. in asp.net mvc application?
  3. are serious?

in asp.net mvc application use controller action return actionresults. return json:

public actionresult foo() {     return json(new[] { "elem1", "elem2" }, jsonrequestbehavior.allowget); } 

then consume controller action using ajax example.

another possibility have array property of view model:

public actionresult foo() {     var model = new myviewmodel      {         somearray = new[] { "elem1", "elem2" }     };     return view(model); } 

and serialize property in view using javascriptserializer:

<script type="text/javascript">     var myarray = <%= new javascriptserializer().serialize(model.somearray) %>;     // todo: can use array native javascript object </script> 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -