部分视图中的模型错误

时间:2018-11-28 11:15:09

标签: c# asp.net-mvc model-view-controller

我正在尝试向我的搜索页面添加分页局部视图。 我之前已将它添加到其他两页中,没有任何问题,但是突然之间,这一错误就出现了。

我收到以下错误:

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

我的控制器

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

我的模型

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

我的视图 我在搜索页面上称部分搜索:

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

我的局部视图

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

我不明白为什么会混淆模型,我在控制器中清楚地设置了模型,并且在调用局部视图时,我也只给出了分页模型而已。

我在做什么错了?

2 个答案:

答案 0 :(得分:2)

在您的@Html.Partial("_pagination", Model.Pagination)中,如果Model.Pagination为空,则Model被传递,而不是正在发生的Model.Pagination

如果new PaginationModel()为空,则可能必须通过Model.Pagination

答案 1 :(得分:0)

在控制器中,您已经创建了SearchViewModel的模型。参见以下行:

var model = new SearchViewModel();

然后,您将其传递给视图,这是错误的。您应该创建PaginationModel模型,因为您的视图需要它。