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 ?