Pavel
Pavel

Reputation: 153

Paging is not working

I saw an article at Microsoft to add paging in c#. Following this article step by step I encountered some difficulties while adapting it to my view. I will try to explain my self better while giving away some of my code:

Controller:

[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));
}

and this is my view:

<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>

Do you guys have any idea on what model component should I pass to the view, regarding the microsoft document. Been stuck here for a while so some elp would be appreciated Thanks!!!

Upvotes: 1

Views: 414

Answers (1)

poke
poke

Reputation: 387507

You do this in your controller:

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

So the paginated list is a nested property SearchMovie of your view model.

But in your view you try this:

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

This won’t work because Model is a SearchViewModel. What you meant to do instead would be the following:

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

Now you are accessing the same property of the model which you previously set the paginated list to.

Upvotes: 1

Related Questions