Good day everybody:
I have an external web api which returns a label, value records needed for a client application
app.MapGet("/AsyncAutocompleteErf/{search}", async (IErvenRepository request, string search) =>
{
var data = await request.GetSearchedErfNumberAsync(search);
if (data == null)
return Results.NotFound("Sorry, there are not records");
return Results.Ok(data);
}).WithName("AsyncAutocompleteErf");
The method returns the following JSON array
[
{
"label": "15209 Gugulethu Infill Housing ",
"value": "1"
},
{
"label": "15211 Edward Avenue",
"value": "3"
}
]
In the client application , I have a method that call the above REST WEB Api
[HttpGet]
public async Task<JsonResult> AutoComplete(string param)
{
{
var httpClient = _httpClientFactory.CreateClient("HousingWebAPI");
using (var response = await httpClient.GetAsync("AsyncAutocompleteErf/" + param))
{
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
var apiResponseBody = JsonConvert.DeserializeObject<IEnumerable<AutoSearchViewModel>>(content);
return Json(apiResponseBody);
}
}
}
return null;
}
Up to this point everything is fine. The script that fires up the Jquery Autocomplete is as follows:
<script>
$( function() {
$("#search" ).autocomplete({
source: function( request, response ) {
$.ajax( {
url: "Erf/AutoComplete",
dataType: "json",
type:"GET",
data: {param:request.term},
success: function( data ) {
response($.map(data, function (item) {
return item;
}));
},
error: function (response)
{
toastr.error(response.responseText);
}
,
failure: function (response)
{
toastr.error(response.responseText);
}
} );
},
minLength: 2
});
} );
</script>
The browser developer tools ( debugger) confirms that the response is [{"label":"15211 Edward Avenue","value":"3"}] when the user types 11 in the search textbox which is the expected value.
The issue is that the label values which in this case is "15211 Edward Avenue "isn't displayed in the text box
<div id="searchBar" tabindex="-1">
<input type="text" name="search" id="search" class="ui-autocomplete" placeholder="Search for Blocks" tabindex="-1">
<ul class="ui-menu" id="searchResults"></ul>
</div>
The above HTML mark up is embedded in a modal pop up dialog. All the required references for making the autocomplete works are in the _Layout.cshtml and not in the pop up dialog ( not sure whether this is the reason why nothing is displayed).
The scripts references I have in the _Layout.cshtml page are:
<script src="https://code.jquery.com/jquery-3.7.1.js"></script>
<script src="https://code.jquery.com/ui/1.14.1/jquery-ui.js"></script>
<partial name="~/Views/UIComponents/_ModalForm.cshtml" />
Any ideas about what is going wrong?
Regards