Sneha K

Sneha K

  • 1.2k
  • 527
  • 195.8k

AutoComplete textbox is not working correctly in mvc5?

Jun 8 2016 8:50 AM
Hi i have one field in my view. That field is Customer it is a dropdown field. In that i have keep dropdown as select option to select the value. But i like to change that field as Autocomplete textbox.
 

enter image description here

In the above image I have customerName field as dropdown field but i keep it by search and select option. But now i change this to autocomplete textbox which is mention the in the below image.

enter image description here

My View Code
 
@Html.Label("Customer Name", new { @class = "control-label" })
@Html.TextBoxFor(Model=>Model.CustomerID)
 
My Jquery Code
 
 
$(function () {
debugger;
$('#CustomerID').autocomplete({
source: function (request, response) {
$.ajax({
url: '@Url.Action("GetVisitCustomer", "VisitorsForm", new { Area = "Sales" })',
type: "GET",
datatype: "json",
data: {
term: request.term
},
success: function (data) {
response($.map(data, function (item) {
return {
label: item.CustomerID,
value: item.DisplayName
}
}))
}
})
},
messages: {
noResults: "", results: ""
}
});
});
 
 My Controller Code
 
public JsonResult GetVisitCustomer(string term)
{
var objCustomerlist = db.Customers.Where(c => c.IsDeleted == false);
var Customerlist = objCustomerlist.Where(c => c.DisplayName.ToUpper().Contains(term.ToUpper())).Select(c => c.DisplayName)).Distinct().ToList();
return Json(Customerlist, JsonRequestBehavior.AllowGet);
}
 

In the above code if i type the letter in textbox means it hit the GetVisitCustomer and calculate the values as per i typed in that text box. Upto this its working fine but after calculating the value in Controller it didnt pass that values to view that it didnt show the appropriate values that is (dropdown box with that calculated values). in the textbox. This is the issue. I tried my level best to explain my issue. Any one help me to resolve this issue and In my url i added new {Areas="Sales"} That is in single application we combined two projects my project is coming in sales under Areas. So only i declare url as like that. 

Advance Thanks..

 

Answers (1)