HelpASisterOut HelpASisterOut - 1 year ago 100
C# Question

Fill JS Array from List<string> C#

I have a

in my Model C# filled on runtime, called
in my Views.

After filling that list I want to fill an array in javascript from values of that list, in order to do the following:

var myArray = [];

//fill myArray with values from @Model.Names

source: myArray

So when the user types in
<input id="tags">
he will get an autocomplete with a list of the names I filled on runtime.

Any idea how to do so?

Answer Source

I prefer this way. No manual Looping, and you can do this with fairly complex viewmodels as well (as long as there are no recursive references within your types).

var myArray = @Html.Raw(Json.Encode(Model.Names))

    source: myArray 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download