如何将jquery自动完成项设置为razor ActionLink助手

时间:2012-01-18 20:50:03

标签: jquery asp.net-mvc asp.net-mvc-3 razor jquery-autocomplete

我在我的应用程序中使用jquery自动完成功能,但我对如何处理单击自动完成项目感到不满意:

$(function () {
        $("input#autocomplete").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '@Url.Action("Search", "Navigation")', type: "POST", dataType: "json",
                    data: { searchTerm: request.term, maxResults: 10 },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { label: item.Name,
                                value: item.CityId
                            }
                        }))
                    }
                })
            },
            select: function (event, ui) {                
                window.location.href = '@Url.Action("Index", "Home")' + '/' + ui.item.value;
            }
        });
    });

这种方法有效,但有时不起作用 有没有办法将@Html.ActionLink...添加为项目?

1 个答案:

答案 0 :(得分:4)

试试这个

function onItemSelect(item){
   var siteRootUrl = '@Url.Content("~/")';
   window.location.href = siteRootUrl + 'controller/action/' + item;
}

$(function () {
        $("input#autocomplete").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '@Url.Action("Search", "Navigation")', type: "POST", dataType: "json",
                    data: { searchTerm: request.term, maxResults: 10 },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { label: item.Name,
                                value: item.CityId
                            }
                        }))
                    }
                })
            },
            select: function (event, ui) {                
                onItemSelect(ui.item.value);
            }
        });
 });