[英]ASP.NET MVC - Model Item passed is Generic.List but requires type PagedList
I'm new to all this and I'm having trouble with Paging in an App.net 4.7.2 MVC application.我对这一切都很陌生,我在 App.net 4.7.2 MVC 应用程序中遇到了分页问题。 I get the following error:我收到以下错误:
"The model item passed into the dictionary is of type 'System.Collections.Generic.List 1[AIAR.Models.PIAModel]', but this dictionary requires a model item of type 'PagedList.IPagedList
1[AIAR.Models.PIAModel]'." "The model item passed into the dictionary is of type 'System.Collections.Generic.List 1[AIAR.Models.PIAModel]', but this dictionary requires a model item of type 'PagedList.IPagedList
1[AIAR.Models.PIAModel] ’。”
I think I understand the problem, as I'm using a generic list within my controller, but I'm just not sure how to resolve it.我想我理解这个问题,因为我在 controller 中使用了一个通用列表,但我只是不确定如何解决它。 I've been looking through all the Googles for some time now and just can't figure it out.我已经浏览了所有的谷歌一段时间了,只是想不通。 Any help would be much appreciated.任何帮助将非常感激。 Please let me know if I need to provide anything else.如果我需要提供其他任何东西,请告诉我。
Controller section: Controller部分:
public ActionResult ViewPIAS(string searchBy, string search,int? page)
{
ViewBag.Message = "PIA List";
var data = LoadPIAS();
List<PIAModel> PIAS = new List<PIAModel>();
foreach (var row in data)
{
PIAS.Add(new PIAModel
{
Id = row.Id,
AssetName = row.AssetName,
AssetDescription = row.AssetDescription,
Unit = row.Unit,
InformationAssetCustodian = row.InformationAssetCustodian
});
}
if (searchBy == "AssetName")
{
return View(PIAS.Where(x => x.AssetName.StartsWith(search)).ToPagedList(page ?? 1, 3).ToList());
}
else if(searchBy == "AssetDescription")
{
return View(PIAS.Where(x => x.AssetDescription.StartsWith(search)).ToPagedList(page ?? 1, 3).ToList());
}
else
return View(PIAS);
}
View:看法:
@model IPagedList<AIAR.Models.PIAModel>
@using PagedList;
@using PagedList.Mvc;
@{
ViewBag.Title = "ViewPIAS";
}
<h2>ViewPIAS</h2>
<p>
@Html.ActionLink("Create New", "NewPIA")
</p>
<p>
@using (Html.BeginForm("ViewPIAS", "PIA", FormMethod.Get))
{
<div>@Html.ActionLink("Return Search Defaults", "ViewPIAS")</div>
<b>Search By:</b>
@Html.RadioButton("searchBy", "AssetName") <text>AssetName</text> @Html.RadioButton("searchBy", "AssetDescription") <text>Asset Description</text><br />
@Html.TextBox("search")<input type="submit" value="Search" />
}
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.First().Id)
</th>
<th>
@Html.DisplayNameFor(model => model.First().AssetName)
</th>
<th>
@Html.DisplayNameFor(model => model.First().AssetDescription)
</th>
<th>
@Html.DisplayNameFor(model => model.First().Unit)
</th>
<th>
@Html.DisplayNameFor(model => model.First().InformationAssetCustodian)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.AssetName)
</td>
<td>
@Html.DisplayFor(modelItem => item.AssetDescription)
</td>
<td>
@Html.DisplayFor(modelItem => item.Unit)
</td>
<td>
@Html.DisplayFor(modelItem => item.InformationAssetCustodian)
</td>
<td>
@Html.ActionLink("Edit", "EditPIA", new { id = item.Id }) |
@Html.ActionLink("Details", "ViewPIA", new { id = item.Id }) |
@Html.ActionLink("Delete", "DeletePIA", new { id = item.Id })
</td>
</tr>
}
</table>
@Html.PagedListPager(Model, page => Url.Action("ViewPIAS", new { page }))
Model: Model:
namespace AIAR.Models
{
public class PIAModel
{
[Display(Name = "Id")]
public int Id { get; set; }
[Display(Name = "Asset Name")]
public string AssetName { get; set; }
[Display(Name = "Asset Description")]
public string AssetDescription { get; set; }
[Display(Name = "Unit")]
public string Unit { get; set; }
[Display(Name = "Information Asset Custodian")]
public string InformationAssetCustodian { get; set; }
}
}
Thanking you!感谢您!
the solution is that you use the IList interface both in controller and in the view, greetings解决方案是您在 controller 和视图中都使用IList接口,问候
Managed to get this working the other day guys.前几天设法让这个工作正常进行。
int recordsPerPage = 10;
var list = PIAS.ToList().ToPagedList(page, recordsPerPage);
if (searchBy == "AssetName")
{
var assetnamesearch = list.Where(x => x.AssetName.Contains(search));
return View(assetnamesearch.ToList().ToPagedList(page, recordsPerPage));
}
else
return View(list);
}
Thanks for all the responses!感谢所有的回复!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.