错误提示信息如下:
The model item passed into the dictionary is of type ‘System.Collections.Generic.List’1[Controllers.TrxLogIndexColumns]’, but this dictionary requires a model item of type ‘PagedList.IPagedList’1[WebLinkAdmin.Controllers.TrxLogIndexColumns]’.
翻译如下: 传递到字典中的模型项的类型’System.Collections.Generic.List’1[Controllers.TrxLogIndexColumns],但此词典需要模型类型为’PagedList.IPagedList`1[Controllers.TrxLogIndexColumns]’. 总结:cshtml页面需要的model类型和Controller传入的类型不一致。
网上查资料获得信息如下图所示:
----------------------------------我的内容如下----------------------------------- Controller中的部分代码如下:
namespace Controllers { public class TrxlogController : BaseController { [UserAuthorizeAttribute] public ActionResult Details(long? id) { IQueryable<V_TRXLOG> trxlogInfo = from p in db.V_TRXLOG where p.TrxLogKey == id select p; IQueryable<TrxLogIndexColumns> trxLogIndexColumns = trxlogInfo.Select(t => new TrxLogIndexColumns() { TrxLogKey = t.TrxLogKey, TerminalSN = t.TerminalSN, TenderType = t.TenderType, TransType = t.TransType, Flag = t.Flag, RequestDate = t.RequestDate, ResponseDate = t.ResponseDate, ResultCode = t.ResultCode, PosRefNum = t.PosRefNum, ResultMessage = t.ResultMessage, RequestContent = t.RequestContent, ResponseContent = t.ResponseContent, }); List<TrxLogIndexColumns> showList = trxLogIndexColumns.OrderByDescending(a => a.RequestDate).ToList(); showList = showList.OrderByDescending(s => s.TrxLogKey).ToList(); //foreach (var trxLog in trxlogInfo) foreach (var trxLog in showList) { } //return View(trxlogInfo); return View(showList); } } public class TrxLogIndexColumns { public long TrxLogKey; //public string POSID; public string TerminalSN; public int CommandType; public int TenderType; public int TransType; public int Flag; public DateTime RequestDate; public DateTime? ResponseDate; public string ResultCode; public string PosRefNum; public string ResultMessage; public string RequestContent; public string ResponseContent; public string CommandTypeShow; public string TenderTypeShow; public string TransTypeShow; } }detail.cshtml中的代码头如下(注意前两行):
@using PagedList @model IPagedList<Controllers.TrxLogIndexColumns> @{ ViewBag.Title = "Transaction Details"; } <script> function changeCursor(obj) { obj.style.cursor = "text"; } </script> <div ></div>视图类似于以下:
<main class="gate-page-main-scoll"> <div class="gate-page-details"> <dt class="col-md-2"> TrxLogKey </dt> <dd class="col-md-4 col-lg-3"> @Html.DisplayFor(model => item.TrxLogKey) </dd> </div> </main>程序运行后报错如下图所示: (截图内容之所以有些许差距是因为我把一些内容删掉了,隐私)
将detail.cshtml中的代码头修改成如下所示即可。
@model IEnumerable<Controllers.TrxLogIndexColumns> @{ ViewBag.Title = "Transaction Details"; } <script> function changeCursor(obj) { obj.style.cursor = "text"; } </script> <div ></div>总结:cshtml页面需要的model类型和Controller返回到达cshtml页面中model的类型不一致;
我这里一开始使用的是 @model IPagedList<Controllers.TrxLogIndexColumns>
后来修改成 @model IEnumerable<Controllers.TrxLogIndexColumns>
即可。