搜索后更改项目时遇到问题。我查看了类似的线程,但没有找到解决方案:(看起来页面第一次加载良好 - 第一次加载整个 Index.cshtml 页面,其中包含所选类别中的书籍集合。页面上有一个搜索引擎 - 在搜索“manual”之后 - ajax 正确地将元素替换为名称中包含“manual”的元素。然后,当我第二次在搜索引擎中输入内容(例如“练习”)时,页面内容不再改变。我尝试调试,发现新项目已从数据库正确下载——条件“if (Request.IsAjaxRequest ())”为真,项目被传递到部分视图——“foreach”循环通过它们。不幸的是,在_Partial 之后,什么也没有发生。我找不到错误 - 最奇怪的是第一个 ajax 调用工作正常 - 只有第二个(和后续)不好。目录控制器.cspublic ActionResult Index(string categoryName = null, string searchQuery = null) { if (categoryName == null) categoryName = (db.Categories.Find(1)).Name; var category = db.Categories.Include("Books").Where(x => x.Name.ToLower() == categoryName).Single(); var books = category.Books.Where(x => (searchQuery == null || x.Title.ToLower().Contains(searchQuery.ToLower()) || x.SubTitle.ToLower().Contains(searchQuery.ToLower()) || x.Level.ToLower().Contains(searchQuery.ToLower())) && !x.Inaccessible); if (Request.IsAjaxRequest()) return PartialView("_PartialBooksList", books); else return View(books); }索引.cshtml<form class="o-search-form" id="search-form" method="get" data-ajax="true" data-ajax-target="#booksList"> <input class="o-search-input" id="search-filter" type="search" name="searchQuery" data-autocomplete-source="@Url.Action("SearchTips")" placeholder="Search" /> <input class="o-search-submit" type="submit" value="" /></form> <div class="row" id="booksList"> @Html.Partial("_PartialBooksList")</div>
2 回答
![?](http://img1.sycdn.imooc.com/5458692c00014e9b02200220-100-100.jpg)
哈士奇WWW
TA贡献1799条经验 获得超6个赞
我一步一步地查看了代码,找到了解决问题的方法。
在第一次搜索中,替换 id="booksList"
<div class="row" id="booksList"> @Html.Partial("_PartialBooksList") </div>
只有没有 id = booksLists 的部分视图。
在接下来的搜索中,这个地方没有ID,也没有什么可以替换的。
![?](http://img1.sycdn.imooc.com/54584f8f00019fc002200220-100-100.jpg)
胡子哥哥
TA贡献1825条经验 获得超6个赞
我不确定是否是这种情况,但尝试更改此代码:
$($targetElement).replaceWith($newContent);
对此:
$($targetElement).html($newContent);
我认为问题在于第一次搜索后替换了 id="booksList" 的 div 元素。所以你在第二次搜索中没有这个元素。
添加回答
举报
0/150
提交
取消