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:
在将脚本与自动完成函数连接到Json控制器时,我遇到了一个问题。视图是一个公式,用户可以在其中插入数据,比如带有datepicker函数的日期和描述问题的一般文本。整个公式是这样的:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
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" })
The txtSearch is connected to my skript SearchUser.js:
txtSearch连接到我的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.
当我使用一个字符串数组作为源时,自动补全将出现。
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 .
JavaScript在视图的顶部注册,jQueryUI在_Layout.cshtml注册。我使用jquery 1.11.3和jqueryui 1.11.4。
In The Controller New1
in the JsonResult you find this:
在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);
}
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/Dialog时?我得到json文件。json文件包含以下内容:["Mueller"]
But when I go to my formula http://localhost:51299/New1/Create and insert "m" into the TextBox nothing happens.
但是,当我转到公式http://localhost:51299/New1/Create并将“m”插入文本框时,什么也没有发生。
So now my question: What can i do to make it work?
现在我的问题是:我能做些什么来让它工作呢?
Update (It's working!!!)
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.
Aaaaah工作! ! !谢谢!他不能使用源文件,所以现在我把它改成了“/New1/Dialog”。我知道这不是使用直接url而不是“@url”的好方法。动作(“对话”,“New1”),但我认为他在“正常”和“正常”之间没有区别。如果你知道我为什么不能使用@url。行动,我会感兴趣的。
View (Create.cshtml)
视图(Create.cshtml)
@Html.TextBox("searchTerm", null, new { id = "searchTerm" })
Skript (SearchUser.js)
Skript(SearchUser.js)
$(function () {
$("#searchTerm").autocomplete({
source: "/New1/Dialog",
minLength: 1
});
});
controller (New1Controller.cs)
控制器(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);
}
1 个解决方案
#1
1
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:
jQueryUI autocomplete使用名称术语(不是搜索)来创建请求。换句话说,当你输入“m”时,它会发送以下请求:
http://localhost:51299/New1/Dialog?term=m
http://localhost:51299 / /对话框?不丹词= m
You should be able to fix this by simply renaming the parameter:
只需重命名参数即可解决此问题:
public JsonResult Dialog(string term)
#1
1
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:
jQueryUI autocomplete使用名称术语(不是搜索)来创建请求。换句话说,当你输入“m”时,它会发送以下请求:
http://localhost:51299/New1/Dialog?term=m
http://localhost:51299 / /对话框?不丹词= m
You should be able to fix this by simply renaming the parameter:
只需重命名参数即可解决此问题:
public JsonResult Dialog(string term)