【问题标题】:The model item passed into the dictionary is of type 'System.Collections.Generic.List`, [closed]传递到字典中的模型项的类型为“System.Collections.Generic.List”,[关闭]
【发布时间】:2014-04-01 14:43:12
【问题描述】:

我正在学习 MVC 中的分页
在索引视图中我有搜索按钮,它应该显示结果分页明智

Home 控制器中的索引方法 ...

 public ActionResult Index(string searchBy, string search ,int? Page)
 {
     if (searchBy == "Gender")
     {
         return View(db.Employees.Where(x => x.Gender == search || search == null).ToList().ToPagedList(Page??1,3)) ;
     }
     else
     {
         return View(db.Employees.Where(x => x.FUllName == search || search == null).ToList());
     }
 }

和索引视图

@using PagedList;
@using PagedList.Mvc; 

@model IPagedList<_SearchDemo.Models.Employee>

    <p>
        @using (Html.BeginForm("Index","Home",FormMethod.Get))
        {
            <b>SearchBy</b> @Html.RadioButton("searchBy", "Name", true)       
            <text>Name</text>
            @Html.RadioButton("searchBy", "Gender") <text>Gender</text>
            <br />
            @Html.TextBox("search") <input type="submit" value="search" />
        }
    </p>
    <table border="1">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.First().FUllName)
            </th>

        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.FUllName)
                </td>

            </tr>
        }
    </table>

在重新加载索引页面时出现错误...

传入字典的模型项是类型 'System.Collections.Generic.List, but this dictionary requires a model item of type 'PagedList.IPagedList1

【问题讨论】:

  • 你为什么不仔细看看错误信息,然后看看那两条return View... 行,我敢打赌你自己就能找到答案。
  • StackOverflow 会在您完成自己可以做的所有事情时为您提供帮助,而不是在您遇到错误时作为您的第一站。特别是这个错误是少数非常具体说明问题所在的错误之一,我相信如果你花点时间,你可以很容易地自己弄清楚。

标签: asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:

我假设您的代码正在到达您的 Action 的 else 部分,当您的视图期望员工的 PagedList&lt;T&gt; 时,它只会返回员工的 List&lt;T&gt;

您可以创建并返回一个接受 List&lt;T&gt; 员工的新视图,或者更改您的代码以返回 .ToPagedList

【讨论】:

    猜你喜欢
    • 2017-04-21
    • 2018-07-24
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 2014-01-02
    相关资源
    最近更新 更多