脚本未连接到JsonResult Controller [英] Script doesnt connect to JsonResult Controller

查看:144
本文介绍了脚本未连接到JsonResult Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将具有自动完成功能的脚本连接到我的Json控制器时遇到问题.该视图是一个公式,用户可以在其中插入数据,例如带日期选择器功能的日期以及用于描述问题的通用文本.整个公式如下:

I have a problem with connecting the script with the autocomplete function to my Json controller. The view is a formula, where the user can insert data, like dates with the datepicker function and general text to describe the issues. The whole formula is in this:

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

所有文本框,DropDownList和编辑器都按以下方式连接到模型:

All Textboxes, DropDownLists and Editors are connected to the model like so:

<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>

此刻,我尝试插入文本框,其中自动填充应按以下方式进行:

At the moment I try to insert the Textbox, where the autocomplete should happen like this:

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

txtSearch已连接到我的skript SearchUser.js:

The txtSearch is connected to my skript SearchUser.js:

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

当我使用源字符串数组时,会显示自动完成功能.

when I use a source an array of strings, the autocomplete appears.

JavaScript已注册在视图顶部,而jQueryUI已注册在_Layout.cshtml中.我正在使用jquery 1.11.3和jqueryui 1.11.4.

the JavaScript is registered on top of the view and jQueryUI is registered in _Layout.cshtml. I am using jquery 1.11.3 and jqueryui 1.11.4 .

在JsonResult的Controller New1中,您会找到以下内容:

In The Controller New1 in the JsonResult you find this:

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"]

when i test the website and look for http://localhost:51299/New1/Dialog?search=m i get the json file. The json file contains this: ["Mueller"]

但是当我转到公式 http://localhost:51299/New1/Create 并插入"m"进入TextBox不会发生任何事情.

But when I go to my formula http://localhost:51299/New1/Create and insert "m" into the TextBox nothing happens.

所以现在我的问题是:我该怎么做才能使其正常工作?

So now my question: What can i do to make it work?

Aaaaah它的工作!!!非常感谢!他无法使用源,所以现在我将其更改为"/New1/Dialog". 我知道这不是使用直接URL而不是'@ url.Action("Dialog","New1")'的好方法,但是我认为他在普通'" . 如果您有一个想法为什么我不能使用@ url.Action,我会对它感兴趣.

Aaaaah its working!!!. Thanks a lot! He couldnt use the source, so now I changed it to "/New1/Dialog". I know it is not a good way to use the direct url instead of '@url.Action("Dialog", "New1")', but i think he couldnt differ between normal ' and ". If you have an Idea why i couldnt use @url.Action, i would be interested in it.

查看(Create.cshtml)

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

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

推荐答案

jQueryUI自动完成功能使用名称term(而不是search)来编写请求.换句话说,当您输入"m"时,它将发送以下请求:

jQueryUI autocomplete is using the name term (not search) to craft a request. In other words, when you type "m", it's sending the following request:

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

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

You should be able to fix this by simply renaming the parameter:

public JsonResult Dialog(string term)

这篇关于脚本未连接到JsonResult Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆