1

tried using the following but it's not the solution

controller:

var list = new [] { "one", "two", "three" };
ViewData["List"] = javaScriptSerializer.Serialize(list);

jquery:

var list = [ '@ViewData["List"]' ];
$("#numbers").autocomplete({
    source: list
});
4
  • no such thing as jquery string list... check the server output for "var list = ..." in view source in the browser Commented Dec 4, 2011 at 21:46
  • it's ['["one","two","three"]']; Commented Dec 4, 2011 at 21:48
  • To make a javascript array on the client side, the output needs to be "["one", "two", "three"]" Commented Dec 4, 2011 at 21:53
  • I realize that, but the javascript serializer didn't output that way Commented Dec 4, 2011 at 21:57

2 Answers 2

3

In your page (presumably razor, based on your syntax), declare your list as so:

var list = @Html.Raw(ViewData["List"]);

In your current implementation the HtmlHelper is encoding your string to display correctly in a webpage, which you obviously do not want (hence the use of HtmlHelper.Raw) and you are also nesting an array within an array.

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

3 Comments

That gave me the following one big long string using the javascript serializer['["ActionScript","AppleScript","Asp","BASIC","C","C++","Clojure","COBOL","ColdFusion","Erlang","Fortran","Groovy","Haskell","Java","JavaScript","Lisp","Perl","PHP","Python","Ruby","Scala","Scheme"]'];
Was that not what you expected?
I figured it out, I had added it as such var list = ['@ViewData... Your example worked thanks.
0

Use this:

var list = [ '@Html.Raw(ViewData["List"])' ];
$("#numbers").autocomplete({
    source: list
});

Comments

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.