【问题标题】:Script doesnt connect to JsonResult Controller脚本未连接到 JsonResult 控制器
【发布时间】:2015-07-26 20:31:25
【问题描述】:

我在将带有自动完成功能的脚本连接到我的 Json 控制器时遇到问题。该视图是一个公式,用户可以在其中插入数据,例如使用 datepicker 函数的日期和描述问题的一般文本。整个公式是这样的:

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

所有文本框、下拉列表和编辑器都连接到模型,如下所示:

<div class="editor-label">
        @Html.LabelFor(model => model.Overview)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Overview)
        @Html.ValidationMessageFor(model => model.Overview)
    </div>

目前我尝试插入文本框,自动完成应该像这样发生:

 <b>Name: </b>
     @Html.TextBox("searchTerm", null, new { id = "txtSearch" })

txtSearch 连接到我的脚本 SearchUser.js:

$(function () {
    $("#txtSearch").autocomplete({
        source: '@url.Action("New1", "Dialog")',
        minLength: 1
    });
});

当我使用源字符串数组时,会出现自动完成。

JavaScript 注册在视图之上,jQueryUI 注册在 _Layout.cshtml 中。我正在使用 jquery 1.11.3 和 jqueryui 1.11.4 。

在 JsonResult 的控制器 New1 中,您会发现:

public JsonResult Dialog(string search)
{
    List<string> users = db
                            .Users
                            .Where(p => p.FirstName.ToLower().Contains(search.ToLower()))
                            .Select(p => p.LastName)
                            .ToList();

    return Json(users, JsonRequestBehavior.AllowGet);
}

当我测试网站并寻找http://localhost:51299/New1/Dialog?search=m 我得到了 json 文件。 json 文件包含以下内容:["Mueller"]

但是当我转到我的公式 http://localhost:51299/New1/Create 并在 TextBox 中插入“m”时没有任何反应。

所以现在我的问题是:我该怎么做才能让它发挥作用?


更新(它正在工作!!!)

啊啊啊它的工作!!!。非常感谢!他无法使用源,所以现在我将其更改为“/New1/Dialog”。 我知道使用直接 url 而不是 '@url.Action("Dialog", "New1")' 不是一个好方法,但我认为他在普通 ' 之间没有区别>”。 如果你知道为什么我不能使用@url.Action,我会对它感兴趣。

查看(创建.cshtml)

@Html.TextBox("searchTerm", null, new { id = "searchTerm" })

脚本 (SearchUser.js)

$(function () {
$("#searchTerm").autocomplete({
    source: "/New1/Dialog",
    minLength: 1
});
}); 

控制器 (New1Controller.cs)

public JsonResult Dialog(string term)
    {
        List<string> users = db
                            .Users
                            .Where(p => p.LastName.ToLower().Contains(term.ToLower()))
                            .Select(x => x.LastName)
                            .ToList(); 

     return Json(users, JsonRequestBehavior.AllowGet);
    }

【问题讨论】:

  • 您是否使用浏览器的开发者工具查看请求是否成功完成?如果你在 Dialog 操作中放置一个断点,它会被命中吗?
  • 是的,我已经这样做了,它没有受到打击。
  • @url.Action("New1", "Dialog") 指向DialogController 中的方法New1()。要在New1Controller 中点击Dialog(),它必须是@url.Action("Dialog", "New1")
  • 谢谢,这也是一个很大的错误,但它也没有解决问题。
  • 啊,我明白了。您不能在 JavaScript 文件中使用 razor 助手,因此永远不会处理 @Url.Action(...)

标签: c# jquery json asp.net-mvc-4 jquery-ui-autocomplete


【解决方案1】:

jQueryUI 自动完成使用名称term(不是search)来制作请求。换句话说,当您输入“m”时,它会发送以下请求:

http://localhost:51299/New1/Dialog?term=m

您应该能够通过简单地重命名参数来解决此问题:

public JsonResult Dialog(string term)

【讨论】:

  • 我改了,但不是缺陷。我现在也改变了:@Html.TextBox("searchTerm", null, new { id = "txtSearch" }) 到 @Html.TextBox("searchTerm", null, new { id = "searchTerm" }) 并且也改变了脚本到 $("#searchTerm").autocomplete({
  • @MimiMüller:如果你在浏览器的开发者工具中查看请求,你会看到什么?有错误吗?
  • 当我将 m 插入文本框中时,会出现:
    没有搜索结果。
    目前我使用的是 google chrome 浏览器
  • @MimiMüller:好的,在 chrome 的“网络”选项卡中,输入字母后你看到了什么?
  • 啊,我可以看到错误 http-fault 404.0 - 未找到。它的名称是:@url.Action(%22Dialog%22,%20%22New1%22)?term=m。类型为:xhr,发起者为 jquery-1.11.3.JS:9664
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-04
  • 1970-01-01
相关资源
最近更新 更多