简体   繁体   中英

How to show autocomplete from more than one column?

This code returns only the autocomplete from one column, and my logic is to get it from two columns. I want to know what is the problem. Please, if any one knows the answer please help me.
Thanks!

    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: ""
        }
    });                  
});`

You have unnecessarily created a whole set of t variables. The request object will have only one term property, which refers to the value currently in the text input. So the request.t will be undefined here

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

Moving forward, in the action method string t will be null and will not be used in filtering

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();

To correct it, in the where clause use term in both conditions and remove unnecessary references to t

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