C# 在构建ASP.NET核心web应用程序时,我得到了;传入字典的模型项的类型为';系统.集合.通用.列表'1'quot;
传递到C# 在构建ASP.NET核心web应用程序时,我得到了;传入字典的模型项的类型为';系统.集合.通用.列表'1'quot;,c#,asp.net-mvc,generics,asp.net-core-mvc,C#,Asp.net Mvc,Generics,Asp.net Core Mvc,传递到ViewDataDictionary的模型项的类型为'WebApp.Models.Order',但此ViewDataDictionary实例需要类型为'System.Collections.Generic.List1[WebApp.Models.Product]'的模型项` 型号 Product.cs namespace WebApp.Models { public class Product { public int ProductId { get; se
ViewDataDictionary
的模型项的类型为'WebApp.Models.Order'
,但此ViewDataDictionary
实例需要类型为'System.Collections.Generic.List
1[WebApp.Models.Product]'的模型项`
型号
Product.cs
namespace WebApp.Models
{
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}
Order.cs
using System.Collections.Generic;
namespace WebApp.Models
{
public class Order
{
public int OrderId { get; set; }
public List<Product> Products { get; set; }
public decimal Total { get; set; }
}
}
处理请求时发生未处理的异常
我不知道如何处理它。视图需要一个产品列表:
@model List<WebApp.Models.Product>
如果视图的其余部分也使用该顺序,则听起来您只需要更改视图上的模型声明:
@model WebApp.Models.Order
并循环浏览该模型上的产品:
@foreach (var Product in Model.Products)
奇怪的是,你试图在布局中这样做。不要那样做。布局只是围绕要应用于每个页面的页面进行修饰。它不需要模型中的任何东西。视图本身应该绑定到模型。您的
\u布局
视图将为每个页面呈现。您是说您的布局需要一个顺序
。因此,按照您的设置方式,返回的每个模型都必须是Order
的子类。视图需要一个列表
,但在控制器中返回一个订单。
@{
Layout = "_Layout";
}
@model List<WebApp.Models.Product>
return View(order);
@model WebApp.Models.Order
@foreach (var Product in Model.Products)