0

I am trying to add a pagination partial view to my search page. I have previously added it to two other pages without any issues, but suddenly this one just bugs out.

I get the following error:

System.InvalidOperationException: 'The model item passed into the dictionary is of type 'Website.Models.SearchViewModel', but this dictionary requires a model item of type 'Website.Models.Helpers.PaginationModel'.'

My Controller

public ActionResult Index(Search currentPage, string query, int pageSize = 10, int page = 1)
        {
            var model = new SearchViewModel();

            if (!string.IsNullOrEmpty(query))
            {// searchquery is present, go for it!

                var searchResults = Umbraco.TypedSearch(query, true, "MyContentSearcher");
                if (searchResults != null && searchResults.Count() > 0)
                {
                    var searchResult = SearchMapper.Map<SearchModel>(searchResults, Umbraco).OrderByDescending(x=> x.CreateDate);


                    // pagination logic
                    pageSize = currentPage.PageSize > 0 ? currentPage.PageSize : pageSize;
                    model.Pagination = new PaginationModel()
                    {
                        PageNumber = page,
                        TotalPages = (int)Math.Ceiling((double)searchResult.Count() / (double)pageSize),
                        Query = query
                    };
                    var skipAmount = page == 1 ? 0 : ((page - 1) * pageSize);

                    // skip and take amount according to pagination.
                    model.SearchResult = searchResult.Skip(skipAmount).Take(pageSize);

            return View(model);
        }

My Model

public class PaginationModel
    {
        public int PageNumber { get; set; }
        public int TotalPages { get; set; }
        public string Query { get; set; }
    }

My View i call the partial on the search page:

@Html.Partial("_pagination", Model.Pagination)

My Partial view

    @model Website.Models.Helpers.PaginationModel

    @if (Model.TotalPages > 1)
    {
        <div class="col-12 margin-top-20">
            <div class="row">
                <div class="col-12">
                    <nav aria-label="...">
                        <ul class="pagination">
                            <li class="page-item @(Model.PageNumber > 1 ? "" : "disabled")">
                                @if (string.IsNullOrEmpty(Model.Query))
                                {
                                    <a class="page-link" href="?page=@(Model.PageNumber-1)" tabindex="-1">
                                        <i class="fa fa-angle-left"></i>
                                        <span class="sr-only">Previous</span>
                                    </a> 
                                } else{
                                    <a class="page-link" href="?query=@(Model.Query)&page=@(Model.PageNumber-1)" tabindex="-1">
                                        <i class="fa fa-angle-left"></i>
                                        <span class="sr-only">Previous</span>
                                    </a>
                                }



                        </li>
                        @for (int i = 1; i <= Model.TotalPages; i++)
                        {
                            <li class="page-item @(Model.PageNumber == i ? "active" : "")">
                                <a class="page-link" href="?page=@i">@i</a>
                            </li>
                        }

                        <li class="page-item @(Model.PageNumber == Model.TotalPages ? "disabled" : "")">
                            <a class="page-link " href="?page=@(Model.PageNumber+1)">
                                <i class="fa fa-angle-right"></i>
                                <span class="sr-only">Next</span>
                            </a>
                        </li>
                    </ul>
                </nav>
            </div>
        </div>
    </div>
}

I dont understand why it confuses the model, i clearly set the model in the controller and when calling the partial view i also only give it the pagination model and no more..

What am i doing wrong ?

andrelange91
  • 1,018
  • 3
  • 21
  • 48

2 Answers2

2

In your @Html.Partial("_pagination", Model.Pagination) if Model.Pagination is null Model gets passed instead of Model.Pagination which is what is occurring.

You may have to pass new PaginationModel() if Model.Pagination is null.

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
  • that removed the error. wierd.. i did not think it could ever be null. – andrelange91 Nov 28 '18 at 11:59
  • It is because it is in `if`(` - you might put that `model.Pagination = new PaginationModel()` just after your model declaration, then set its value as you have it. OR just have `SearchViewModel` constuctor do that – Mark Schultheiss Nov 28 '18 at 12:03
  • To avoid the query null or empty you might also create a constructor for `new PaginationModel()` setting its values like the query to a non-null value. (i.e. a default value), lots of ways to consider that scenario resolution. – Mark Schultheiss Nov 28 '18 at 12:10
0

In the controller, you have created the model of SearchViewModel. See below line:

var model = new SearchViewModel();

Then, you are passing this to view which is wrong. You should create a model of PaginationModel because your view requires it.

Keyur Ramoliya
  • 1,900
  • 2
  • 16
  • 17
  • I create a pagination model and set it to the searchviewmodel here: model.Pagination = new PaginationModel() { PageNumber = page, TotalPages = (int)Math.Ceiling((double)searchResult.Count() / (double)pageSize), Query = query }; – andrelange91 Nov 28 '18 at 11:27
  • @andrelange91 In that case, check your parameter `query` is null or not. – Keyur Ramoliya Nov 28 '18 at 11:37
  • i have a check on it, @if (string.IsNullOrEmpty(Model.Query)) – andrelange91 Nov 28 '18 at 11:44