当我进入下一页时,Umbraco搜索分页不起作用

时间:2018-10-02 03:51:19

标签: javascript jquery .net razor umbraco

我已尝试在搜索结果中进行分页,代码如下

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
 <form action="/movesearchresult/" method="GET">
        <input type="text" class="searchfield" placeholder="" name="query">
        <button class="searchbtn btn btn-default btn-sm">Search</button>
    </form>

     @{
        var searchQuery = Request.QueryString["query"];

    } 

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

        //required results per page
        var pageSize = 5;

        //retrieve current page from query string
        int currentPage = 1;
        if (!String.IsNullOrEmpty(Request.QueryString["query"]))
        {
            int.TryParse(Request.QueryString["query"], 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>
    <div class="searchresults move-repo">
    <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>
    </div>

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

在第一次搜索enter image description here上,分页看起来像这样。

但是当我通过单击分页到达下一页时,出现错误屏幕 enter image description here是哪个人可以让我知道我在想什么

1 个答案:

答案 0 :(得分:1)

我在代码中看到2个问题:

以下内容:

sudo

应该是

    if (!String.IsNullOrEmpty(Request.QueryString["query"]))
    {
        int.TryParse(Request.QueryString["query"], out currentPage);
    }

您到下一页的链接应包含查询:

    if (!String.IsNullOrEmpty(Request.QueryString["page"]))
    {
        int.TryParse(Request.QueryString["page"], out currentPage);
    }

请注意,您将首先过滤@searchQuery以避免xss攻击

我还建议在umbraco中实现搜索时使用名为ezsearch的程序包,该程序包简单且构建良好