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

发布于 2024-12-10 10:27:17 字数 1994 浏览 0 评论 0原文

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

HomeControllerMethod

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

        return Json(accounts, JsonRequestBehavior.AllowGet);
    }

About.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); ;
                }
            });
        } 
    });
});

I'm having trouble with my auto complete functionality, it hits the controller and returns the values but nothing is displayed on the page, I have provided the code below any help is appreciated.

HomeControllerMethod

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

        return Json(accounts, JsonRequestBehavior.AllowGet);
    }

About.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>
} 

Edit:
Below is my final function

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳公公是暖光 2024-12-17 10:27:17

一些事情:

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

    $('#searchTerm').autocomplete({
        来源:函数(请求,响应){
            $.ajax({
                url: '@Url.Action("GetAccounts", "Home")',
                数据: { id: request.term },
                数据类型:'json',
                类型:'发布',
                成功:函数(数据){
                    响应(数据); // 根据您的数据,您可能需要在此处执行后处理。
                }
            });
        },
        最小长度:3
    });
    
  2. 此外,请确保您为小部件提供了它期望的数据。您需要为 response 函数提供一个字符串数组,例如:

    <前><代码>[“项目1”,“项目2”,“项目3”]

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

    [{ 标签:“Item1”,值:“1” },{ 标签:“Item2”,值:“2” }]
    

    您可能已经这样做了,但我必须查看您的控制器操作返回的内容。

A few things:

  1. You need to call the response function that the widget supplies to the source function you provide. Also, it looks like you have one of the autocomplete's options (minLength) mixed in with the AJAX call:

    $('#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. Additionally, make sure that you're supplying the widget with the data it expects. You need to supply the response function with an array of strings, e.g:

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

    Alternatively, you can supply an array of objects with a label property, a value property, or both:

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

    You may already be doing this, but I would have to see what your controller action returns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文