自动完成窗口小部件始终将null参数传递给控制器

时间:2018-04-08 17:48:26

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

我遇到了JQuery Autocomplete小部件的问题。搜索参数始终将null传递给控制器​​。甚至参数名称在视图和控制器中也是相同的。

这是我的控制器:

public JsonResult Blist(string term)
{
    try
    {
       var fullPath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data/BankList.csv");

       List<string> master = System.IO.File.ReadAllLines(fullPath).ToList();
       master = System.IO.File.ReadAllLines(fullPath).Where(s => s.Trim() != string.Empty).ToList();

       var rowmaster = (from dr in master

       let parts = dr.Split(",".ToCharArray())

       select new { name = parts[1], code = parts[0] }).ToList();
            PromotersListscs prolist = new PromotersListscs();

            return Json(rowmaster, JsonRequestBehavior.AllowGet);
        }
        catch (UnauthorizedAccessException UAEx)
        {
            return null;
        }
    }

我的观点:

@using (Html.BeginForm("Blist", "ControlerName", FormMethod.Get))
{
    @Html.TextBox("term", null, new { id = "txtSearch" })
    <input type="submit" value="Search" />   
}

使用Javascript:

<script>
      $(function () {
          $("#txtSearch").autocomplete({
              source: '@Url.Action("Blist", "ControlerName")',
              minLength: 0
          });
      });
        </script>

我试过了:

@Html.TextBox("term", null, new { id = "txtSearch", formaction = @Url.Action("Blist", "POPM_Ref_PromotersInfo"), formmethod = "Get" })

仍然传递null。

1 个答案:

答案 0 :(得分:0)

似乎你没有将searchterm传递给控制器​​动作。试试这个

$(function () {
    $("#txtSearch").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/ControlerName/Blist",
                dataType: "json",
                data: { term: request.term },
                success: function (data) {    
                    response(data);                    }
            });
        }
    });
});