0

I have an Html.Textbox that I allow the user to type a customer name into. Once they reach 3 or more characters I call an autocomplete function which returns a list of customers in a dropdown. When they make a selection I would like to make another ajax call which would return a new view. My problem is when I add the second call the data from the autocomplete is removed. This is what I have so far:

 $("#CustomerSearch_FullName").autocomplete({ minLength: 3, fontSize: '10px', source: function (request, response) { $.ajax({ url: '@Url.Action("CustomerNameAutoComplete", "Customer")', async: false, data: { searchstring: $.trim(request.term) }, success: function (data) { response($.map(data, function (item) { $('#CustomerIdDisplay').val(item.id); $('#customer-select').val(item.id); $('#Order_Customer_FirstName').val(item.firstname); $('#Order_Customer_Email').val(item.email); return { label: item.choice, id: item.id }; })); }, }); }, select: function (event, ui) { id = ui.item.id; $.ajax ({ url: '@Url.Action("Create", "Order")', data: { customerId: id } }); 
1
  • you mention when they make a selection you want to fire the second call. why don't you put the second ajax call in a jquery change event? Commented Oct 22, 2014 at 19:47

1 Answer 1

1

You only need to add return false to your select statement:

, select: function (event, ui) { id = ui.item.id; $.ajax ({ url: '@Url.Action("Create", "Order")', data: { customerId: id } }); return false; } 

The default action of the auto-complete on 'select' is to set the textbox value with whatever you select from the auto-complete box, if you are not ready to set the textbox because you want to do another call base on the selection, you have to add return false to prevent the default behavior. After your second call finished you have to set the textbox whit what ever you want based on your result.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.