jquery - MVC3 Razor jQuery 自动完成传递返回值但不显示任何内容

标签 jquery asp.net-mvc-3 razor jquery-autocomplete

我的自动完成功能遇到问题,它会命中 Controller 并返回值,但页面上没有显示任何内容,我已在下面提供了代码,如有任何帮助,我们将不胜感激。

HomeController方法

[HttpPost]
    public JsonResult  GetAccounts(string id)
    {
        var accounts = NavRepository.GetAccountsBasedOnString(id);

        return Json(accounts, JsonRequestBehavior.AllowGet);
    }

关于.cshtml

 <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/base/jquery-ui.css" type="text/css" media="all" /> 
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript">      </script>
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")" type="text/javascript">      </script>


<script type="text/javascript">
$(function () {
    $('#searchTerm').autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '@Url.Action("GetAccounts", "Home")',
                data: { id: request.term },
                dataType: 'json',
                type: 'POST',
                minLength: 3,
                success: function (event, ui) {
                    searchTerm.valueOf (ui.item.value);
                }
            });
        } 
    });
});

</script>

@using (Html.BeginForm())
{               
<form method="post" action="">
 <input id="searchTerm" name="searchTerm" type="text" />
     <input type="submit" value="Go" />
 </form>
} 

编辑: 下面是我的最终功能

  $(function () {
    $('#searchTerm').autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '@Url.Action("GetAccounts", "Home")',
                data: { id: request.term },
                dataType: 'json',
                type: 'POST',
                minLength: 3,
                success: function (data) {
                    response(data); ;
                }
            });
        } 
    });
});

最佳答案

一些事情:

  1. 您需要调用小部件向您提供的source 函数提供的response 函数。另外,您似乎将自动完成选项之一 (minLength) 与 AJAX 调用混合在一起:

    $('#searchTerm').autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '@Url.Action("GetAccounts", "Home")',
                data: { id: request.term },
                dataType: 'json',
                type: 'POST',
                success: function (data) {
                    response(data); // You may have to perform post-processing here depending on your data.
                }
            });
        },
        minLength: 3
    });
    
  2. 此外,请确保您向小部件提供了它期望的数据。您需要为 response 函数提供一个字符串数组,例如:

    ["Item1", "Item2", "Item3"]
    

    或者,您可以提供带有 label 属性、value 属性或两者的对象数组:

    [{ label: "Item1", value: "1" }, { label: "Item2", value: "2" }]
    

    您可能已经这样做了,但我必须看看您的 Controller 操作返回什么。

关于jquery - MVC3 Razor jQuery 自动完成传递返回值但不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7816303/

相关文章:

c# - 如何使绑定(bind)到 Nullable<int> 属性的 DropDownListFor 接受空值?

c# - ASP.NET Core 3.0 策略重定向

c# - 如何获取 Razor 代码中选定单选按钮的 ID?

asp.net-mvc-3 - RAZOR/MVC 3 中 View 的预处理等效项

javascript - 信息窗口和地理编码器

javascript - html anchor 标记的 onclick 不起作用

javascript - 在 jquery 表中查找特定值

asp.net-mvc-3 - 使用 MVC 3 ASP.net 连接到 SQL Server 2008

asp.net-mvc-3 - 将@helper 代码转移到 App_Code 文件夹抛出错误

javascript - 如何使用jquery的ajax方法将json字符串传输到另一个页面?