[英]Ajax in MVC 6 - making search function
I have a new project and decided to go with c# .net 6 MVC in VS2022...我有一个新项目,并决定 go 与 c# .net 6 MVC in VS2022 ...
In may old projects this code works flawless.在可能的旧项目中,此代码完美无缺。
@section Scripts
{
<script type="text/javascript">
$("#Klijent_Name").autocomplete({
source: function (request, response) {
$.ajax({
url: "@Url.Action("SearchKlijenti")",
type: "POST",
dataType: "json",
data: { term: request.term },
success: function (data) {
response($.map(data, function (item) {
return { label: item.label, value: item.label, id: item.id };
}))
}
})
},
minLength: 1,
select: function (event, ui) {
$("#KlijentId").val(ui.item.id);
$("#KlijentLabel").html(ui.item.label);
$("#SearchKupac").val("");
return false;
}
});
</script>
}
and latest variation of controller endpoint:和 controller 端点的最新变体:
public JsonResult SearchKlijenti(string term)
{
var klijentVM = _klijent.Search(term);
if (klijentVM != null)
{
var items = klijentVM.Select(x => new { id = x.KlijentId, label = x.FriendlyName });
return new JsonResult(Ok(items));
}
return new JsonResult(Ok());
}
Using latest jQuery 3.6.1, and bootstrap 5.2.0.使用最新的 jQuery 3.6.1 和引导程序 5.2.0。 Tried using jquery-ui.js, jquery.unobtrusive-ajax.js...尝试使用 jquery-ui.js、jquery.unobtrusive-ajax.js ......
Problem is that the call is not triggered , or not finding it's way to controller action.问题是呼叫没有被触发,或者没有找到 controller 行动的方式。 Have tried putting alert();试过把 alert(); and omitting data manipulation and calls, but still nothing.并省略数据操作和调用,但仍然没有。 When testing jQuery:测试 jQuery 时:
$("SearchKupac").keyup(function() {
alert();
});
works.作品。
Tried to debug using Firefox, but either I don't know to use it, or the call is not triggered.尝试使用Firefox进行调试,但要么不知道要使用,要么调用未触发。
I don't know where and what to look anymore...我不知道在哪里看什么了...
EDIT: Here is also HTML snippet编辑:这也是 HTML 片段
<label asp-for="Klijent.Name">Ime</label>
<input class="form-control ajax" asp-for="Klijent.Name" />
<span asp-validation-for="Klijent.Name" class="text-danger"></span>
I also tried selecting with $("input.ajax")... Tried double and single quotes.我还尝试使用 $("input.ajax")... 尝试选择双引号和单引号。 Bare in mind, this is a working code from MVC 5 project.请记住,这是来自 MVC 5 项目的工作代码。 It doesn't work in new project它在新项目中不起作用
If you want to use keyup event,here is a demo:如果你想使用 keyup 事件,这里有一个演示:
<input id="SearchKupac" />
js: js:
$("#SearchKupac").keyup(function() {
alert();
});
If the id of input is SearchKupac
,you need to use $("#SearchKupac")
in js.Also,you can use $("#SearchKupac")
with autocomplete.如果 input 的 id 是SearchKupac
,你需要在 js 中使用$("#SearchKupac")
。另外,你可以使用$("#SearchKupac")
自动完成。
@section Scripts
{
<script type="text/javascript">
$("#SearchKupac").autocomplete({
source: function (request, response) {
$.ajax({
url: "@Url.Action("SearchKlijenti")",
type: "POST",
dataType: "json",
data: { term: request.term },
success: function (data) {
response($.map(data, function (item) {
return { label: item.label, value: item.label, id: item.id };
}))
}
})
},
minLength: 1,
select: function (event, ui) {
$("#KlijentId").val(ui.item.id);
$("#KlijentLabel").html(ui.item.label);
$("#SearchKupac").val("");
return false;
}
});
</script>
}
So Firefox developer tool was of no help.所以 Firefox 开发工具没有帮助。 Crome developer tool find an error. Crome 开发者工具发现错误。 It was not apparent immediately, but the jquery-ui.js (of version 1.13.2 in my case) resolved the issue.虽然不是很明显,但是 jquery-ui.js(在我的例子中是 1.13.2 版本)解决了这个问题。
<script src="~/js/jquery-ui-1.13.2/jquery-ui.min.js"></script>
I have tested it before using the same jquery-ui.js and it was not working.在使用相同的 jquery-ui.js 之前,我已经对其进行了测试,但它无法正常工作。 Maybe VS 2022 did not load it properly, I do not know but it is working now.也许 VS 2022 没有正确加载它,我不知道,但它现在正在工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.