Viewmodel想要IEnumerable

时间:2014-02-28 22:13:52

标签: c# asp.net-mvc

当我尝试返回viewmodel时出现此错误。

传递到字典中的模型项是'Tp1WebStore3.ViewModels.ShoppingCartViewModel'类型,但是这个字典需要一个类型为'System.Collections.Generic.IEnumerable`1 [Tp1WebStore3.Models.Panier]'的模型项。

知道问题是什么?

PanierController.cs

 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
         }

ShoppingCartViewModels.cs

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using Tp1WebStore3.Models;

 namespace Tp1WebStore3.ViewModels
 {
     public class ShoppingCartViewModel
     {
         public List<Panier> CartItems { get; set; }
         public decimal CartTotal { get; set; }
     }
 }

2 个答案:

答案 0 :(得分:0)

您尚未在视图中正确定义模型。

试试这个。把它放在你的视野之上。

@model Tp1WebStore3.ViewModels.ShoppingCartViewModel

您会在Index.cshtml文件夹中找到Views视图文件。

答案 1 :(得分:0)

给出错误的读数。它告诉你非常简单的英语究竟出了什么问题。您正在查看想要一个类型为Painer的IEnumerable,但您正在传递ShoppingCartViewModel。

切换

@model IEnumerable<Painer>

@model ShoppingCartViewModel
在你看来

。命名空间与我上面写的不匹配,但这应该在你的视图的第1行。

相关问题