2014-02-28 63 views
-2

我得到這個錯誤,當我想返回viewmodel。Viewmodel想要IEnumerable

傳入字典的模型項目類型爲'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; } 
    } 
} 
+1

你不明白哪部分錯誤? –

回答

0

您在您查看有沒有正確定義的模型。

試試這個。把它放在你的視野之上。

@model Tp1WebStore3.ViewModels.ShoppingCartViewModel 

你會發現在Views文件夾Index.cshtml視圖文件。

+0

當我把這行放在ShoppingCartViewModel.cs的頂部時,它給了我一個錯誤,namspace不能直接包含成員,如字段或方法 – user3127986

+0

@ user3127986:這不是你的看法,你會在視圖中找到你的Index.cshtml文件夾。 –

+0

@ user3127986:任何運氣?這個答案對你有幫助嗎? –

0

給錯誤一個很好的閱讀。它用簡單的英文告訴你到底出了什麼問題。您正在查看需要一個類型爲Painer的IEnumerable,但您正在傳入ShoppingCartViewModel。

開關

@model IEnumerable<Painer> 

@model ShoppingCartViewModel 
在你看來

。命名空間不符合我上面寫的內容,但這應該在你的視圖的第1行。

相關問題