如何显示来自多个列的自动填充功能?

时间:2014-09-01 12:18:03

标签: c# asp.net-mvc jquery-ui-autocomplete

此代码仅返回一列中的自动完成,我的逻辑是从两列中获取它。我想知道问题是什么。如果有人知道答案,请帮帮我 谢谢!

    public JsonResult SearchFromTable(string name)
    {            
        AccountingDataLayer.AccountingSMSEntities context = new AccountingDataLayer.AccountingSMSEntities();
        var search = (from s in context.Products
                          where s.Name == name || s.FullCode == name
                          select new{s.FullCode , s.Name , s.Description });

        return Json(search, JsonRequestBehavior.AllowGet);
    }

    public JsonResult AutoCompleteProductName(string term , string t)
    {
        AccountingDataLayer.AccountingSMSEntities context = new AccountingDataLayer.AccountingSMSEntities();
        var result = (from p in context.Products
                      where p.Name.ToLower().Contains(term.ToLower()) || p.FullCode.ToLower().Contains(t.ToLower())
                      select new { p.Name , p.FullCode }).Distinct();
        return Json(result, JsonRequestBehavior.AllowGet);
    } 

    $("#search").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/InVoiceStock/AutoCompleteProductName",
                type: "POST",
                dataType: "json",
                data: { term: request.term , t: request.t },
                success: function (data) {
                    response($.map(data, function (item , t) {
                        return { label: item.Name, value: item.Name, label: t.FullCode, value: t.FullCode };
                    }))
                }
            })
        },
        messages: {
            noResults: "", results: ""
        }
    });                  
});`

1 个答案:

答案 0 :(得分:0)

您已经不必要地创建了一整套t变量。请求对象只有一个term属性,该属性引用文本输入中当前的值。因此request.t将在此处未定义

data: { term: request.term , t: request.t }

继续前进,在操作方法string t中将为null,不会用于过滤

var result = (from p in context.Products
    where p.Name.ToLower().Contains(term.ToLower()) || p.FullCode.ToLower().Contains(t.ToLower())
    select new { p.Name , p.FullCode }).Distinct();

要更正它,在where子句中在两个条件中都使用term并删除对t的不必要引用