asp.net - jquery UI autocomplete list from SQL Server query in cshtml -


i following jquery ui autocomplete template (https://jqueryui.com/autocomplete/#default) trying pull data autocomplete sql server db query.

it looks when javascript need availabletags javascript array have form ["abc", "def", "ghi",..."] based on in jquery ui demo source code.

var availabletags = [    "actionscript",    "applescript",    "asp",    "basic",     "..."]; 

my code gets me list this: abc, def, ghi,... . i'm not sure if shows {abc, def, ghi,...} or [abc, def, ghi...] when pass javascript variable.

here code list db

@{ list<string> availabletags = new list<string>(); foreach (var item in db.query("select tag my_tags_table"))    {     var tag = item.tag ;     availabletags.add(tag);     string tagstring = (string.join(", ", availabletags.select(x => x.tostring()).toarray()));    }   } 

debugging shows tagstring created list abc, def, ghi, ... mentioned need add double quotes in there. here how pass tagstring javascript array/variable.

 <script>       $(function () {       var availabletags = '<%=tagstring%>';             $("#tags").autocomplete({                  source: availabletags              });       });  </script> 

then here's input box.

 <div class="ui-widget">     <input id="tags">  </div>     

can me 1) add double quotes in , 2) else required availabletags show proper javascript array?

jquery ui autocomplete needs source property value array. string.join method going return string value separated commas item1, item2, item3.

what need array.

@{   list<string> availabletags = new list<string>();   foreach (var item in db.query("select tag my_tags_table"))   {     var tag = item.tag ;     availabletags.add(tag);          }  } 

and in script section in same view, can convert c# varibale (availabletags list) javascript array , use that.

$(function () {      var availabletags = @html.raw(newtonsoft.json.jsonconvert                                                 .serializeobject(availabletags.toarray()));     $("#tags").autocomplete({ source: availabletags }); }); 

Comments