The model item passed into the dictionary is of type ‘System.Collections.Generic.List`1[

错误提示信息如下:

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>

即可。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值