System.InvalidOperationException 正在尝试分页
System.InvalidOperationException while trying paging
当我尝试使用索引视图时出现此错误
谁能告诉我该怎么做
"The model item passed into the dictionary is of type
'System.Collections.Generic.List1[MvcCRUDSearching.Models.Customer]',
but this dictionary requires a model item of type
'PagedList.IPagedList1[MvcCRUDSearching.Models.Customer]'."
我的代码-
客户控制器
{
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>
我是新开发学习,语法写不好,
谢谢
问题每个 if else 分支,你 return 不同类型的模型
第一个和第二个你 return IPagedeList<Customer>
输入,但最后一个你 return List<Customer>
当您的控制器转到其他分支时,它无法转换为 IPagedList<MvcCRUDSearching.Models.Customer>
作为 cshtml 文件中的模型类型。
您应该使用如下 ToPagedList
方法
else
{
return View(dbModel.Customers.ToList().ToPagedList(page ?? 1, 4));
}
当我尝试使用索引视图时出现此错误
谁能告诉我该怎么做
"The model item passed into the dictionary is of type 'System.Collections.Generic.List1[MvcCRUDSearching.Models.Customer]', but this dictionary requires a model item of type 'PagedList.IPagedList1[MvcCRUDSearching.Models.Customer]'."
我的代码-
客户控制器
{
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>
我是新开发学习,语法写不好,
谢谢
问题每个 if else 分支,你 return 不同类型的模型
第一个和第二个你 return IPagedeList<Customer>
输入,但最后一个你 return List<Customer>
当您的控制器转到其他分支时,它无法转换为 IPagedList<MvcCRUDSearching.Models.Customer>
作为 cshtml 文件中的模型类型。
您应该使用如下 ToPagedList
方法
else
{
return View(dbModel.Customers.ToList().ToPagedList(page ?? 1, 4));
}