尝试分页时出现System.InvalidOperationException

时间:2019-12-18 08:54:04

标签: asp.net asp.net-mvc entity-framework linq crud

当我尝试索引视图给我这个错误

有人可以告诉我该怎么做

  

“传递到字典中的模型项是类型   'System.Collections.Generic.List1 [MvcCRUDSearching.Models.Customer]',   但是此字典需要类型为的模型项   'PagedList.IPagedList1 [MvcCRUDSearching.Models.Customer]'。”

我的代码-

CustomerController

{
    public class CustomerController : Controller
    {
        // GET: Customer
        public ActionResult Index(string searchBy, string search, int? page)
        {
            using (DbModels dbModel = new DbModels())
            {
                if(searchBy=="Department")
                {
                    var dep = dbModel.Customers.Where(x => x.Department == search).ToList().ToPagedList(page ?? 1, 4);
                    return View(dep);
                }
                else if(searchBy=="Name")
                {
                    var nam = dbModel.Customers.Where(y => y.Name.StartsWith(search)).ToList().ToPagedList(page ?? 1, 4);
                        return View(nam);
                }
                else
                {
                    return View(dbModel.Customers.ToList());
                }
            }

        }

Index.cshtml

@using PagedList.Mvc;
@model PagedList.IPagedList<MvcCRUDSearching.Models.Customer>
@using PagedList;
@{
    ViewBag.Title = "Index";
}

<div>
    <h2>Index</h2>

    <p>
        @Html.ActionLink("Create New", "Create")
    </p>



    <p>
        @using (@Html.BeginForm("Index", "Customer", FormMethod.Get))
        {
            <b> Search By:</b>
            @Html.RadioButton("searchBy", "Name", true)<text>Name</text>
            @Html.RadioButton("searchBy", "Department")<text>Department</text>
            @Html.TextBox("search") <input type="submit" value="Search" class="btn" />
        }
    </p>


    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.First().Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.First().Department)
            </th>
            <th></th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Department)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
                    @Html.ActionLink("Details", "Details", new { id = item.ID }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.ID })
                </td>
            </tr>
        }

    </table>
    @Html.PagedListPager(Model, page => Url.Action("Index", new { page }))
</div>

我正在学习新的开发人员,很抱歉编写语法,

谢谢

1 个答案:

答案 0 :(得分:1)

如果每个其他分支都出现问题,则返回不同类型的模型 您返回的第一个和第二个IPagedeList<Customer>类型,但最后一个返回的是List<Customer>

当控制器转到else分支时,它不能作为cshtml文件中的模型类型转换为IPagedList<MvcCRUDSearching.Models.Customer>

您应使用如下的ToPagedList方法

else
{
     return View(dbModel.Customers.ToList().ToPagedList(page ?? 1, 4));
}