0

I am implementing a simple JQuery autocomplete with few entries so I don't need to use a web method. The code is like this:

 $( function() {
    var availableTags = [
      "ActionScript",
      "AppleScript"     
    ];
    $( "#tags" ).autocomplete({
      source: availableTags
    });
  } );

The entries availableTags are unique to the user so they need to come from the server. How should I get the entries from the server side to the JavaScript in ASP.NET Core?

2 Answers 2

1

This assumes you have some service IUserTagService like this, registered through dependency injection:

public interface IUserTagService
{
    IList<string> GetUserTags();
}

Since you don’t want (or need) an AJAX call here to load the tags after the page has been rendered, we’re just going to render the tags directly into the output. For that, we basically convert the returned list from the GetUserTags method into JSON and put this into a script tag. So the end result will look mostly like your static example.

So in the .cshtml view, we first inject our service. We can use the @inject directive at the beginning of the file for this:

@inject IUserTagService userTagService;

Then, we simply open a script tag and write the output into a variable:

<script>
$(function() {
    var availableTags = @Json.Serialize(userTagService.GetUserTags());
    $("#tags").autocomplete({
        source: availableTags
    });
});
</script>

This uses the Json.Serialize which is a utility function that is available in views to serialize the list into JSON.

Sign up to request clarification or add additional context in comments.

Comments

1

You can make an ajax call to your server action method which returns the list as JSON array.

$(function () {

    $.getJSON("@Url.Action("GetItems")", function (availableTags) {
        $( "#tags" ).autocomplete({
            source: availableTags
        });
    });

})

Assuming your GetItems action method returns the list of items as json array

public JsonResult GetItems()
{
    var list = new List<string>() {"ActionScript","AppleScript"};
    return new JsonResult(list);
}

Another option is to load the data (list of strings to your main views GET action method and in the razor view, convert that to a JS Array

So in your GET action,

 var list = new List<string>() {"ActionScript","AppleScript"};
 ViewBag.Items = list;
 return View();

and in the razor view,

@section Scripts
{
    <script>
    $(function() {
        var availableTags = @Html.Raw(Newtonsoft.Json.JsonConvert
                                 .SerializeObject(ViewBag.Items));
        $( "#tags" ).autocomplete({
            source: availableTags
        });
    });
    </script>
}

Dependency injection is possible in asp.net core views now. poke's answer above uses that. You may consider doing that instead of the ViewBag solution above.

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.