jquery autocomplete using mvc3 dropdownlist

后端 未结 3 1105
粉色の甜心
粉色の甜心 2021-01-06 09:04

I am using ASP.NET MVC3 with EF Code First. I have not worked previously with jQuery. I would like to add autocomplete capability to a dropdownlist that is bound to my model

3条回答
  •  粉色の甜心
    2021-01-06 09:43

    This is what I did FWIW.

    $(document).ready(function () {
        $('#CustomerByName').autocomplete(
        {
            source: function (request, response) {
                $.ajax(
                {
                    url: "/Cases/FindByName", type: "GET", dataType: "json",
                    data: { searchText: request.term, maxResults: 10 },
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        response($.map(data, function (item) {
                            return {
                                label: item.CustomerName,
                                value: item.CustomerName,
                                id: item.CustomerID
                            }
                        })
                        );
                    },
    
                });
            },
            select: function (event, ui) {
                        $('#CustomerID').val(ui.item.id);
                    },
            minLength: 1
        });
    
    });
    

    Works great!

提交回复
热议问题