【问题标题】:The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[DB_Payment.Models.GetInvoice]'传入字典的模型项的类型为“System.Collections.Generic.List`1[DB_Payment.Models.GetInvoice]”
【发布时间】:2021-10-16 08:53:45
【问题描述】:

在 C# MVC 应用程序中出现错误:

传入字典的模型项的类型为“System.Collections.Generic.List`1[DB_Payment.Models.GetInvoice]”,但此字典需要“DB_Payment.Models.GetInvoice”类型的模型项。

控制器:

  List<DB_Payment.Models.GetInvoice> Items = new List<DB_Payment.Models.GetInvoice>();
DataSet ds = new DataSet();
ds = GetItems(customer_code);
if (ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
var chk = new GetInvoice();
chk.customer_code = dr["customer_code"].ToString();
chk.Date_Due = dr["Date_Due"].ToString();
chk.amount = dr["amount"].ToString();
chk.amt_paid = dr["amt_paid"].ToString();
Items.Add(chk);

             }
         }
         return View(Items);

查看代码:

 @model IEnumerable<DB_Payment.Models.GetInvoice>
@using GridMvc.Html
@using System.Web.Mvc.Ajax

@{

 ViewBag.Title = "grid";
 WebGrid grid = new WebGrid(Model, rowsPerPage: 100);
 Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-sm-6">
<h4 class="card-title mb-4">Inventory</h4>
</div>

             </div>
             <div id="webgrid">
                 @Html.Grid(Model).Columns(columns =>
                 {
                     columns.Add(m => m.customer_code).Titled("customer_code");                       
                     columns.Add(m => m.Date_Due).Titled("Date_Due");
                     columns.Add(m => m.amount).Titled("Amount");
                     columns.Add(m => m.amt_paid).Titled("Amt_Paid");
                 }
                 )
             </div>
         </div>
     </div>
 </div>
</div>

有人可以帮忙吗?

【问题讨论】:

  • 我假设 GetInvoice 是一个类。您可以发布 GetInvoice 课程吗?
  • 您的视图名称是什么,您的操作名称是什么?

标签: asp.net-mvc model-view-controller


【解决方案1】:

在视图中,将行从@model IEnumerable 更改为@model List

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    相关资源
    最近更新 更多