分页不起作用

时间:2018-05-01 22:08:11

标签: c# asp.net entity-framework asp.net-core

我看到了article at Microsoft to add paging in c#。按照这篇文章的步骤,我在适应我的观点时遇到了一些困难。我将尝试更好地解释自己,同时放弃我的一些代码:

控制器:

[HttpGet("searchmovie")]
public IActionResult Search(string option, string searchmovie, int? page, string currentFilter)
{
    if (search != null)
    {
        page = 1;
    }
    else
    {
        search = currentFilter;
    }
    SearchViewModel data = new SearchViewModel();
    int pageSize = 4;
    IEnumerable<SearchDataItemViewModel> searchMovie =
    Mapper.Map<IEnumerable<SearchDataItemViewModel>>(_unitOfWork.Movies.GetByString(search));
    var searchMoviesP = Helpers.PaginatedList<SearchDataItemViewModel>.CreateAsync(searchMovie,page ?? 1, pageSize);
    data.SearchMovie = searchMoviesP;
    return View("Search", data));
}

这是我的观点:

<li class="tab-2 tabs-list-item">
    <ul>
        <li>
            <h3 class="tabs-header">LoremIpsum</h3>
            <ul class="content-full col-1">
                @foreach (var mv in Model.SearchMovie)
                {
                    if (mv != null)
                    {
                        <li>
                            <div class="to-do-desc">
                                <p>@mv.Text</p>
                            </div>
                        </li>
                    }
                    else
                    {
                        <li><p>No results</p></li>
                    }
                }
            </ul>
        </li>
    </ul>
    {
        //Website.Helpers.PaginatedList<Website.ViewModels.SearchDataItemViewModel> PaginatedModel = Model as Website.Helpers.PaginatedList<Website.ViewModels.SearchDataItemViewModel>;
        //this is what I tried but of course the there was an error of type :
        //Cannot convert type 'Website.ViewModels.SearchViewModel' to 'Website.Helpers.PaginatedList<Website.ViewModels.SearchDataItemViewModel>

        string prevDisabled = !PaginatedModel.HasPreviousPage ? "disabled" : "";
        ..string nextDisabled = !PaginatedModel.HasNextPage ? "disabled" : "";
    }

    <a asp-controller="Movies" asp-action="Search"
       asp-route-page="@(PaginatedModel.PageIndex - 1)"
       asp-route-currentFilter="@ViewData["FilterParam"]"
       class="btn btn-default @prevDisabled">
        Previous
    </a>

    <a asp-controller="Movies" asp-action="Search"
       asp-route-page="@(PaginatedModel.PageIndex + 1)"
       asp-route-currentFilter="@ViewData["FilterParam"]"
       class="btn btn-default @nextDisabled">
        Next
    </a>
</li>

对于微软文档,您是否知道我应该将哪些模型组件传递给视图?被困在这里一段时间,所以一些elp将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:1)

您可以在控制器中执行此操作:

var searchMoviesP = Helpers.PaginatedList<SearchDataItemViewModel>.CreateAsync(searchMovie,page ?? 1, pageSize);
data.SearchMovie = searchMoviesP;

因此,分页列表是视图模型的嵌套属性SearchMovie

但是在你看来你试试这个:

Model as Website.Helpers.PaginatedList<Website.ViewModels.SearchDataItemViewModel>

这不起作用,因为ModelSearchViewModel。你打算做的是:

Model.SearchMovie as Website.Helpers.PaginatedList<Website.ViewModels.SearchDataItemViewModel>

现在您正在访问先前将分页列表设置为的模型的相同属性。