C# Viewmodel需要IEnumerable
我在尝试返回viewmodel时遇到此错误 传入字典的模型项的类型为“Tp1WebStore3.ViewModels.ShoppingCartViewModel”,但此字典需要类型为“System.Collections.Generic.IEnumerable`1[Tp1WebStore3.Models.Panier]”的模型项 你知道问题出在哪里吗 PanierController.csC# Viewmodel需要IEnumerable,c#,asp.net-mvc,C#,Asp.net Mvc,我在尝试返回viewmodel时遇到此错误 传入字典的模型项的类型为“Tp1WebStore3.ViewModels.ShoppingCartViewModel”,但此字典需要类型为“System.Collections.Generic.IEnumerable`1[Tp1WebStore3.Models.Panier]”的模型项 你知道问题出在哪里吗 PanierController.cs namespace Tp1WebStore3.Controllers { public cl
namespace Tp1WebStore3.Controllers
{
public class PanierController : Controller
{
Tp1WebStoreDBEntities db = new Tp1WebStoreDBEntities();
//
// GET: /ShoppingCart/
public ActionResult Index()
{
var cart = ShoppingCart.GetCart(this.HttpContext);
// Set up our ViewModel
var viewModel = new ShoppingCartViewModel
{
CartItems = cart.GetCartItems(),
CartTotal = cart.GetTotal()
};
// Return the view
return View(viewModel); <== the error occurs here
}
namespace Tp1WebStore3.Controllers
{
公共类PanierController:控制器
{
Tp1WebStoreDBEntities db=新的Tp1WebStoreDBEntities();
//
//购买购物车/
公共行动结果索引()
{
var cart=ShoppingCart.GetCart(this.HttpContext);
//设置我们的视图模型
var viewModel=新ShoppingCartViewModel
{
CartItems=cart.GetCartItems(),
cartotal=cart.GetTotal()
};
//返回视图
返回视图(viewModel);您在视图中没有正确定义模型
试试这个,把它放在你的视野上面
@model Tp1WebStore3.ViewModels.ShoppingCartViewModel
您将在Views
文件夹中找到您的Index.cshtml
视图文件。仔细阅读错误。它用非常简单的英语告诉您错误所在。您的视图想要一个IEnumerable类型的Painer,但您正在传递一个ShoppingCartViewModel
开关
@model IEnumerable<Painer>
在您的视图中。名称空间与我上面写的不匹配,但这应该在视图的第1行。您不理解错误的哪一部分?当我将这一行放在ShoppingCartViewModel.cs的顶部时,它给了我一个错误,一个名称空间不能直接包含成员,例如字段或method@user3127986:那不是你的观点,你会发现视图文件夹中的ur Index.cshtml文件。@user3127986:运气好吗?这个答案对你有帮助吗?
@model Tp1WebStore3.ViewModels.ShoppingCartViewModel
@model ShoppingCartViewModel