找不到匹配项-ASP.NET MVC jQuery自动完成

时间:2018-11-10 19:09:55

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

我有一个用于搜索的Autocomplet文本框,当我尝试搜索后会给No matches found提供一些帮助。我也进行了调试,可以看到值将传递给controller.did我错过了一些东西!任何人都可以请我帮忙或指出正确的方向。 在此先感谢:)

Contoller:

  public ActionResult Sp(string searchString) {

        string EmailID = Session["Email"].ToString();

       var v = (from cbr in db.Contact_Business_Relation
                 join c in db.Contact on cbr.Contact_No_ equals c.Company_No_
                 join sa in db.Sales_Invoice_Header on cbr.No_ equals sa.Sell_to_Customer_No_
                 join sih in db.Sales_Invoice_Line on sa.No_ equals sih.Document_No_


                 where c.E_Mail == EmailID
                 &&
                 sih.Type == 2


                 select new ClosedOrders
                 {
                     SalesInvoiceQuantity = db.Sales_Invoice_Line.Where(l => l.Document_No_ == sa.No_).Select(l => l.Quantity).DefaultIfEmpty(0).Sum(),
                     CreatedDate = sa.Posting_Date,
                     DeliveryAddress = sa.Ship_to_Address + ", " + sa.Ship_to_Post_Code + " " + sa.Ship_to_City + ", " + sa.Ship_to_Country_Region_Code,
                     Reference = sa.External_Document_No_,
                     OrderNumber = sa.Order_No_,
                     Fakturanummer = sa.No_,
                     Total = 0,
                     AntalAfsendteVarer = 0,
                     AntalVarer = 0,
                     varnummer = db.Item_Ledger_Entry.Where(s => s.Item_No_ == sih.No_).Select(s => s.Item_No_).Distinct().ToList(),
                     SerialNoInvoiceOrdrelineDeliveryCloses = db.Item_Ledger_Entry.Where(s => s.Item_No_ == sih.No_ && s.Entry_Type == 1 && s.Source_Type == 1 && s.Document_Type == 1).Select(s => s.Serial_No_).Distinct().ToList()
                 });


        if (!String.IsNullOrEmpty(searchString))
        {
            v = v.Where(s => s.Fakturanummer.Contains(searchString));


        }

        if (!String.IsNullOrEmpty(searchString))
        {
            v = v.Where(s => s.varnummer.Contains(searchString));

        }

        return Json(v, JsonRequestBehavior.AllowGet);

JavaScript:

<div>
    <label>Search</label>
    <input id="searchInput" />

</div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>


    <script>
        $("#searchInput").autocomplete({
            source: function (request, response) {
                $.ajax({

                    url: '@Url.Action("Sp","Account")',
                    dataType: "json",
                    data: { searchString: $("#searchInput").val() },
                    success: function (data) {

                        if (!data.length) {
                            var result = [
                                {
                                    label: 'No matches found',
                                    value: response.term
                                }
                            ];
                            response(result);
                        }
                        else {
                            // normal response
                            response($.map(data, function (item) {
                                return {
                                    label: item.varnummer,
                                    value: item.varnummer
                                }
                            }));
                        }
                    },

                    error: function (xhr, status, error) {
                        alert("Error");
                    }
                });
            }
        });
    </script>

0 个答案:

没有答案