Jquery 通过自动完成的搜索引擎无法在_Layout.cshtml上运行
我刚刚创建了一个搜索引擎,用户可以过滤产品的名称。在这个时候,我只是在搜索页面。我想知道如何在\u Layout.cshtml上使用Ajax方法设置Jquery 通过自动完成的搜索引擎无法在_Layout.cshtml上运行,jquery,asp.net-mvc-4,razor,autocomplete,search-engine,Jquery,Asp.net Mvc 4,Razor,Autocomplete,Search Engine,我刚刚创建了一个搜索引擎,用户可以过滤产品的名称。在这个时候,我只是在搜索页面。我想知道如何在\u Layout.cshtml上使用Ajax方法设置搜索功能。我已经把它放进去了,但它没有显示任何建议 SearchAjax方法: <script type="text/javascript"> $(document).ready(function () { $("#SearchString").autocomplete({
搜索功能。我已经把它放进去了,但它没有显示任何建议
Search
Ajax方法:
<script type="text/javascript">
$(document).ready(function () {
$("#SearchString").autocomplete({
source: function (request, response) {
$.ajax({
url: "/Home/Find",
data: "{ 'prefixText': '" + request.term + "' }",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
success: function (data) {
response($.map(data, function (item) {
return {
label: item.value,
value: item.value,
id: item.id,
name: item.name
}
}))
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
},
minLength: 2,
select: function (even, ui) {
$("#hidSearch").val(ui.item.id);
//doSearch(ui.item.id);
}
});
$("#btnSearch").click(function(){
var id = $("#hidSearch").val();
doSearch(id);
});
});
function doSearch(id) {
window.location.href = '/Home/Details?id=' + id;
}
控制器:
public ActionResult Index()
{
return View();
}
[HttpPost]
public JsonResult Find(string prefixText)
{
var suggestedProducts = from x in db.Products
where x.Name.StartsWith(prefixText)
select new
{
id = x.ProductID,
value = x.Name + ", ",
name = x.Name
};
var result = Json(suggestedProducts.Take(5).ToList());
return result;
}
public ActionResult Details(int id = 0)
{
Product product = db.Products.Find(id);
if (product == null)
{
return HttpNotFound();
}
return View(product);
}
欢迎回答,谢谢阅读我的问题 您是否在浏览器的开发工具(如Firebug)中看到任何错误
假设您的JavaScript代码是正确的,并且确实正确执行了搜索,请检查该脚本是否在包含jQuery包之后出现(即@Scripts.Render(“~/bundles/jQuery”)
等)
编辑:
您应该尝试在浏览器中调试JavaScript(放置断点、查看控制台等)。您还可以尝试在/Home/Find
控制器方法中放置断点,以查看它是否接收到任何请求
以下是我不久前如何制作自动完成搜索框的示例:
$("#SearchObjects").autocomplete({
source: function (request, response) {
$.ajax({
url: '@Url.Action("AutocompleteObjects", "ObjectController")',
type: "GET",
dataType: "json",
data: { term: request.term },
success: function (data) {
response($.map(data, function (item) {
return { Name: item.Name, ID: item.ID, value: item.Name };
}))
},
error: function(...){...}
})
},
minLength: 3,
select: function (event, ui) {
var selecteditem = ui.item;
// What to do when the user selects something from the suggestions (...)
}
});
编辑2:
我想试试这样的东西:
(... _Layout.cshtml ...)
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
<Your autocomplete script here> // (Preferably by including an external JS file)
(…_Layout.cshtml…)
@Scripts.Render(“~/bundles/jquery”)
@RenderSection(“脚本”,必需:false)
//(最好包括一个外部JS文件)
相关:
如果您查看浏览器的开发工具(如Firebug),是否有任何错误?请将控制器的操作也放入。另外,将浏览器的反应(可能是错误)放在这个页面上应该很有用。@AmirHosseinMehrvarzi我已经编辑过了!我检查过了,还是不起作用。是否有其他解决方案?调试器控制台中没有错误?您的控制器方法(/Home/Find)被调用了吗?在我的Autocompletes的AJAX调用中,我通常使用GET请求。我对它进行了调试,共有5个变量。它可以在搜索
页面中使用,但在\u Layout.cshtml
中无法使用。请参见编辑。我认为您应该只在包含jQuery、剑道等之后才包含自动完成脚本。看起来您并不是这样做的。
(... _Layout.cshtml ...)
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
<Your autocomplete script here> // (Preferably by including an external JS file)