简体   繁体   中英

Autocomplete Dropdown in mvc4?

Hi I have one field called CustomerName it is drop down. I want to make this field as Autocomplete drop down instead of selecting the value . But i can't get any idea to do this task . i am getting lot of confusions so please any one help me to solve this issue

My Controller code

public JsonResult GetCustomers()
{
    return Json(db.Customers.ToList(), JsonRequestBehavior.AllowGet);
}

My View Code

@Html.Label("Customer Name", new { @class = "control-label" })
@Html.DropDownListFor(model => model.CustomerID, new SelectList(string.Empty, "Value", "Text"), "Please select a Customer", new { @class = "form-control required", type = "text" })

My J-query Code

$(function () {
    $.ajax(
        '@Url.Action("GetCustomers", "VisitorsForm")',{
         type: "GET",
         datatype: "Json",
         success: function (data) {
             $.each(data, function (index, value) {
                 $('#CustomerID').append('<option value="' + value.CustomerID + '">' + value.DisplayName + '</option>');
             });
         }
    });
});

This is my code it will load the CustomerName in CustomerName Field from Db.it is perfectly working . But i want to make this field as Autocomplete Dropdown.I tried many ways but no use no one is working for this task. Please any one help me to complete this Autocomplete dropdown task.

Advance Thanks..

why are you giving type = "text" for a dropdown. Actually you need to use jquery-autocomplete . Follow the tutorial: http://www.tutorialspoint.com/jqueryui/jqueryui_autocomplete.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM