gpt4 book ai didi

javascript - Umbraco网站如何使用razor实现分页搜索

转载 作者:行者123 更新时间:2023-11-30 05:31:47 28 4
gpt4 key购买 nike

我正在 Umbraco 中使用 razor 实现搜索。我正在尝试对搜索结果实现分页 - 我使用了 MVCPagedList 但它不起作用。我也尝试了一些 javascript 代码,但这也不起作用。

我的代码:

@using Examine.LuceneEngine.SearchCriteria
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@using PagedList
@using PagedList.Mvc;

@{
int? page = 10;

//PerformSearch performSearch = new PerformSearch();
var pageNumber = page ?? 1;
var searchTerm = Request.QueryString["s"];
if (String.IsNullOrWhiteSpace(searchTerm))
{
searchTerm = "";
}

var searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
var searchCriteria = searcher.CreateSearchCriteria(UmbracoExamine.IndexTypes.Content);
Examine.SearchCriteria.IBooleanOperation filter = null;
var searchKeywords = searchTerm.Split(' ');

int i = 0;
for (i = 0; i < searchKeywords.Length; i++)
{
if (filter == null)
{
filter = searchCriteria.GroupedOr(new string[] { "nodeName", "bodyText", "browserTitle", "tags", "mediaTags" }, searchKeywords[i]);
}
else
{
filter = filter.Or().GroupedOr(new string[] { "nodeName", "bodyText", "browserTitle", "tags", "mediaTags" }, searchKeywords[i]);
}
}

var searchResults = searcher.Search(searchCriteria).Where(r => r["__IndexType"] == "content").ToList();

//performSearch.searcher = searchResults.ToString();
var paging = searchResults.ToPagedList(pageNumber,1);

if (searchResults.Any())
{
<ul>

@foreach (var result in searchResults)
{
var node = Umbraco.TypedContent(result.Id);

var pathIds = result["__Path"].Split(',');

var path = Umbraco.TypedContent(pathIds).Where(p => p != null).Select(p=> new {p.Name}).ToList();

<li style="line-height: 15px; list-style: none;">
<section>
<a href="@node.Url">

<p class="search-heading custom ">@node.Name</p>

<p class="search-link">www.addingvalue.webdevstaging.co.uk<strong>@node.Url</strong></p>
</a>

@if (result.Fields.ContainsKey("title"))
{
<p class="results-title"><strong>@result["title"]</strong></p>
}

@if (result.Fields.ContainsKey("bodyText"))
{
<p>@result["bodyText"].Truncate(250)</p>
}

</section>

</li>
}
</ul>
}
else
{
<p>
There are no results matching your search criteria:

@if (!String.IsNullOrWhiteSpace(searchTerm))
{
<text>'@searchTerm'</text>
}
</p>
}

Html.PagedListPager((IPagedList)paging, paged => Url.Action("List", new { page }));
}

最佳答案

创建分页列表的一种方法,使用 searchResults 作为搜索结果列表:

@{
//number of results
var resultsCount = searchResults.Count();

//required results per page
var pageSize = 5;

//retrieve current page from query string
int currentPage = 1;
if (!String.IsNullOrEmpty(Request.QueryString["page"]))
{
int.TryParse(Request.QueryString["page"], out currentPage);
}

//number of pages
int pageCount = 1;
if (resultsCount > pageSize)
{
pageCount = (int)Math.Ceiling((double)resultsCount / pageSize);
}


}

<div>
Page @currentPage of @pageCount pages
</div>
<ul>
@*Iterate through the search results skipping the results showing on any previous pages, and taking enough results to fill the page*@
@foreach (var result in searchResults.Skip((currentPage - 1) * pageSize).Take(pageSize))
{
<li>
<a href="@result.Url">@result.Name</a>
</li>
}
</ul>

<ul>
@for (int i = 1; i <= pageCount; i++)
{
<li>
<a href="/search-results?page=@i">Page @i</a>
</li>
}
</ul>

关于javascript - Umbraco网站如何使用razor实现分页搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26489314/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com