0

I can't seem to get this to work, I have also tried using a string and string list but I didn't have any luck with that either.

@{
    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>

The error I get:

JavaScript critical error at line 70, column 39 in http://localhost:42697/Units

SCRIPT1002: Syntax error

In the code it shows:

<script>
$(function() {
    var availableTags = System.String[];
$( "#tags" ).autocomplete({
source: availableTags
});

What am I doing wrong here?

4

2 回答 2

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>
于 2013-07-05T18:23:55.830 回答
0
<script>
$(function() {
var availableTags = =[<%foreach (var s in myArray){%>'<%=s%>',<%}%>];
$( "#tags" ).autocomplete({
    source: availableTags
});
});
</script>
于 2013-07-05T18:32:43.017 回答