简体   繁体   English

在ASP.NET MVC错误中创建自动完成

[英]Creating an autocomplete in ASP.NET MVC error

I am trying to create an autocomplete textbox in ASP.NET MVC using jquery autocomplete. 我试图使用jquery自动完成在ASP.NET MVC中创建自动完成文本框。

This is my Index.cshtml: 这是我的Index.cshtml:

<div class="autocomplete">
    @Html.TextBox("item", null, new { id = "autocomplete-textbox", @class = "form-control" });
    <input type="submit" value="Submit" id="autocomplete-button"/>
</div>

<script>
    $(function() {
        $('#autocomplete-textbox').autocomplete({
            source: '@Url.Action("AutoComplete")',
            minlength: 1
        });
    });
</script>

And this is my home controller method: 这是我的家庭控制器方法:

public JsonResult AutoComplete(string item)
    {

        IEnumerable<string> itemsList = new[] { "Ana", "are", "mere", "pere", "papaia", "Aaa", "Ab", "An" };
        IEnumerable<string> filteredResults = null;

        if (string.IsNullOrEmpty(item))
        {
            filteredResults = itemsList;
        }
        else
        {
            filteredResults = itemsList.Where(s => s.IndexOf(item, StringComparison.InvariantCultureIgnoreCase) >= 0);
        }

        return Json(filteredResults, JsonRequestBehavior.AllowGet);
    }

My problem is that the parameter item in the JsonResult AutoComplete is always null and so I always get as a JSON response the whole list. 我的问题是JsonResult AutoComplete中的参数项始终为null,因此我总是得到整个列表的JSON响应。 What can I do ? 我能做什么 ?

Thanks, Marcus 谢谢,马库斯

By default, the jQuery auto complete plugin will send the typed in value with a query string param called term , not item( You can see this if you open up your browsers dev tools-> network tab ) . 默认情况下,jQuery auto complete插件将使用名为term的查询字符串参数发送输入的值,而不是item( 如果打开浏览器开发工具 - >网络选项卡,则可以看到此信息 )。

So change your server action method parameter name to term 因此,将服务器操作方法参数名称更改为term

public JsonResult AutoComplete(string term)
{
  // use term for your checkings
  // to do : Return something    
}

Also, i noticed another issue in your code. 此外,我注意到您的代码中的另一个问题。 If the term variable is not empty, you need to set the filtered results (The result of your Where method call) to the filteredResults variable because that it what you are returning. 如果术语变量不为空,则需要将筛选结果(Where方法调用的结果)设置为filteredResults变量,因为它是您返回的内容。

if (string.IsNullOrEmpty(term))
{
    filteredResults = itemsList;
}
else
{
    filteredResults = itemsList
             .Where(s => s.IndexOf(term, StringComparison.InvariantCultureIgnoreCase) >= 0);
}

Just that you have to name the parameter as "term" that you are using in your controller, as jquery autocomplete widget by default sends the value in a variable named as "term". 只是你必须将参数命名为你在控制器中使用的“术语”,因为默认情况下jquery autocomplete小部件会在名为“term”的变量中发送值。 Therefore just change as below: 因此,只需更改如下:

public JsonResult AutoComplete(string term) { public JsonResult AutoComplete(string term){

    IEnumerable<string> itemsList = new[] { "Ana", "are", "mere", "pere", "papaia", "Aaa", "Ab", "An" };
    IEnumerable<string> filteredResults = null;

    if (string.IsNullOrEmpty(term))
    {
        filteredResults = itemsList;
    }
    else
    {
        filteredResults = itemsList.Where(s => s.IndexOf(term, StringComparison.InvariantCultureIgnoreCase) >= 0);
    }

    return Json(filteredResults, JsonRequestBehavior.AllowGet);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM