![](/img/trans.png)
[英]ASP.NET MVC: The model item passed into the dictionary is of type 'ClassA', but this dictionary requires a model item of type 'ClassA'
[英]ASP.NET MVC - Model Item passed is Generic.List but requires type PagedList
我對這一切都很陌生,我在 App.net 4.7.2 MVC 應用程序中遇到了分頁問題。 我收到以下錯誤:
"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] ’。”
我想我理解這個問題,因為我在 controller 中使用了一個通用列表,但我只是不確定如何解決它。 我已經瀏覽了所有的谷歌一段時間了,只是想不通。 任何幫助將非常感激。 如果我需要提供其他任何東西,請告訴我。
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);
}
看法:
@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:
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; }
}
}
感謝您!
解決方案是您在 controller 和視圖中都使用IList接口,問候
前幾天設法讓這個工作正常進行。
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);
}
感謝所有的回復!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.