如何使用razor在Umbraco站点中实现分页搜索

时间:2014-10-21 14:34:43

标签: javascript jquery asp.net-mvc razor umbraco7

我正在使用Umbraco中的剃刀进行搜索。我正在尝试在搜索结果上实现分页 - 我使用了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 }));
}

1 个答案:

答案 0 :(得分:1)

创建分页列表的一种方法,使用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>