我似乎无法让它工作,我也尝试使用字符串和字符串列表,但我也没有任何运气。
@{
List<string> yyy = new List<string>();
foreach (var x in Model)
{
yyy.Add(x.Name);
}
string[] myArray = yyy.ToArray();
}
<script>
$(function() {
var availableTags = @myArray;
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
我得到的错误:
JavaScript critical error at line 70, column 39 in http://localhost:42697/Units
SCRIPT1002: Syntax error
在代码中显示:
<script>
$(function() {
var availableTags = System.String[];
$( "#tags" ).autocomplete({
source: availableTags
});
我在这里做错了什么?
答案 0 :(得分:2)
尝试将数组编码为json:
<script>
$(function() {
var availableTags = @Html.Raw(Json.Encode(myArray));
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
应该如下所示:
<script>
$(function() {
var availableTags = ["name1", "name2", "name3", etc];
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
答案 1 :(得分:0)
<script>
$(function() {
var availableTags = =[<%foreach (var s in myArray){%>'<%=s%>',<%}%>];
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>