Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
1. My HTML modal: note #search is my id of the textbofor


Java
 $('#search').autocomplete({
                source: function (request, response) {                   
                    var autocompleteUrl = '~/BookingManagement/AutoCompleteCustomer';                
                    $.ajax({
                        url: autocompleteUrl,
                        type: 'GET',
                        cache: false,
                        dataType: 'json',
                        success: function (json) {                          
                            response($.map(json, function (data, id) {
                                return {                                
                                    value: data.LastName
                                };                            
                            }));
                        },
                        error: function (xmlHttpRequest, textStatus, errorThrown) {
                            console.log('some error occured', textStatus, errorThrown);
                        }
                    });
                },
                minLength: 2,
                select: function (event, ui) {
                    alert('you have selected ' + ui.item.label + ' ID: ' + ui.item.value);
                    $('#search').val(ui.item.label);
                    return false;
                }
        });


2. My Controller

   [HttpGet]  
        public async Task<iactionresult> AutoCompleteCustomer(int id)
        {
            try
            {
                List<getcustomermasterfiletbl> CodeListing = new List<getcustomermasterfiletbl>();
                using (var httpClient = new HttpClient())
                {
                    using (var response = await httpClient.GetAsync(apiBaseUrl + "/AutoCompleteCustomer?id=" + id))
                    {
                        string apiResponse = await response.Content.ReadAsStringAsync();
                        CodeListing = JsonConvert.DeserializeObject<list<getcustomermasterfiletbl>>(apiResponse)!;
                    }
                }
                var model = new GetCustomerMasterFileTbl
                {
                    CustomerMasterFileJVM = CodeListing,
                     LastName = CodeListing.FirstOrDefault()?.LastName,
                };

              

                return View(model);
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine($"Processing failed: {ex.Message.ToString()}");
                throw;
            }
        }
    }


What I have tried:

3. During runtime my controller didnt pass throught my script
4 no value diplay in my textboxfor
Posted
Updated 24-Jul-23 4:41am
v3
Comments
Graeme_Grant 24-Jul-23 7:10am    
Flagged as unclear/incomplete as you're too lazy to post any/all questions correctly. Stop wasting our time.

1 solution

Quote:
JavaScript
var autocompleteUrl = '~/BookingManagement/AutoCompleteCustomer';
In JavaScript, that specifies a URL relative to the current URL.

For example, if you were on:
https://yoursite/some-controller/some-action/
the JavaScript would try to load the autocomplete items from:
https://yoursite/some-controller/some-action/~/BookingManagement/AutoCompleteCustomer

If you want to specify an app-relative path within your view, you need to use the Url.Content helper:
JavaScript
var autocompleteUrl = '@Url.Content("~/BookingManagement/AutoCompleteCustomer")';

If it's an external .js file, you won't be able to use any HTML helpers, so you'll need to find an alternative approach.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900