jQuery autocomplete ASP.Net not working

时间:2016-07-25 11:33:54

标签: jquery asp.net

I have the following code in controller:

public JsonResult FindProduct(string searchString)
{
        var productsName = from c in db.Products
                           where c.ProductName.Contains(searchString)
                       select new { value = c.Barcode, label = c.ProductName };

        return this.Json(productsName, JsonRequestBehavior.AllowGet);
}

And the following code in my View:

<script type="text/javascript">
    $(document).ready(function () {
        $("#productName").autocomplete({
            source: '<%= Url.Action("FindProduct") %>',
            select: function (event, ui) {
                var prodBarcode = ui.item.value;
                $("#productName").val(ui.item.label);
                $("#productBarcode").val(ui.item.value);
                return false;
            }
        });
    });
</script>

 <div class="ui-widget"><input type="text" name="productName" id="productName" /></div>

For some reason the FindProduct(string searchString) is never called. And the autocomplete is not working. Do I need something else in my script?

1 个答案:

答案 0 :(得分:-1)

这对我来说似乎是非常错误的。

None

我不知道它是否可行,但是如果我必须检查为什么你的服务器方法从未被调用过,那么我将从那里开始。

将您的整个网址放在source属性中,不要混用服务器和客户端代码。