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?

2 Answers 2

2

try encoding your array to json:

<script>
$(function() {
    var availableTags = @Html.Raw(Json.Encode(myArray));
    $( "#tags" ).autocomplete({
        source: availableTags
    });
});
</script>

which should then look like:

<script>
$(function() {
    var availableTags = ["name1", "name2", "name3", etc];
    $( "#tags" ).autocomplete({
        source: availableTags
    });
});
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

THANK YOU SO MUCH. I have seriously spent the past like 2 hours trying to get this tiny thing to work in 100 different ways and nothing was taking. You are a lifesaver.
0
<script>
$(function() {
var availableTags = =[<%foreach (var s in myArray){%>'<%=s%>',<%}%>];
$( "#tags" ).autocomplete({
    source: availableTags
});
});
</script>

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.