【发布时间】:2018-10-30 20:16:53
【问题描述】:
当我调试我的项目并将商品添加到购物车时,我遇到了这个错误,不知道该怎么办?
System.InvalidCastException: '无法转换类型的对象 'System.Collections.Generic.List
1[ShoppingCartApp.Classes.CartItem]' to type 'System.Collections.Generic.List1[ShoppingCartApp.Controllers.CartItem]'。'
在我看来是@foreach (CartItem cartItem in (List<CartItem>)Session["shoppingCart"])这一行抛出的错误
这是我的控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShoppingCartApp.ServiceReference1;
using ShoppingCartApp.Classes;
public class ShoppingCartController : Controller
{
Service1Client wcf = new Service1Client(); // Connection to WCF
// GET: ShoppingCart
public ActionResult ShoppingCart()
{
return View("ShoppingCart");
}
private int checkIfExisting(int id) // Checks session for existing products, increases quantity if product exists
{
List<CartItem> shoppingCart = (List<CartItem>)Session["shoppingCart"];
for (int x = 0; x < shoppingCart.Count; x++)
if (shoppingCart[x].Product.ProductID == id)
return x;
return -1;
}
public ActionResult RemoveFromCart(int id)
{
List<CartItem> shoppingCart = new List<CartItem>();
shoppingCart.Remove(new CartItem(wcf.GetProduct(id), 1));
Session["shoppingCart"] = shoppingCart;
return View("ShoppingCart");
}
public ActionResult AddToCart(int id) // Creates new session if one is not existing, adds item to cart, uses checkifexisting method to increase quantity
{
if(Session["shoppingCart"] == null)
{
List<CartItem> shoppingCart = new List<CartItem>();
shoppingCart.Add(new CartItem(wcf.GetProduct(id), 1));
Session["shoppingCart"] = shoppingCart;
} else
{
List<CartItem> shoppingCart = (List <CartItem>)Session["shoppingCart"];
int index = checkIfExisting(id);
if (index == -1)
shoppingCart.Add(new CartItem(wcf.GetProduct(id), 1));
else
shoppingCart[index].Quantity++;
Session["shoppingCart"] = shoppingCart;
}
return View("ShoppingCart");
}
public ActionResult Checkout()
{
OrderDetail orderDetail = wcf.GetOrderDetail();
return View();
}
}
这是类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ShoppingCartApp.ServiceReference1;
namespace ShoppingCartApp.Classes
{
public class CartItem
{
private Product product = new Product();
public Product Product
{
get { return product; }
set { product = value; }
}
private int quantity;
public int Quantity
{
get { return quantity; }
set { quantity = value; }
}
public CartItem(Product product, int quantity)
{
this.product = product;
this.Quantity = quantity;
}
}
}
它到底要我做什么?
【问题讨论】:
-
ShoppingCartApp.Classes.CartItem不是ShoppingCartApp.Controllers.CartItem。您是否在 2 个不同的命名空间中添加了相同的模型? -
我在控制器上下文中没有找到任何关于
ShoppingCartApp.Controllers.CartItem的信息,它是在视图@model指令中定义的,例如@model ShoppingCartApp.Controllers.CartItem? -
实际发生的事情是我将 CartItem 类移动到我的控制器文件夹,并删除了我的 Classes 文件夹。我无法让视觉工作室识别这个动作。我删除了 .suo 文件并清除了仍然没有解决问题的 Visual Studio 缓存,Visual Studio 似乎认为那个文件夹仍然存在。所以我继续重新制作文件夹并将 CartItem 类移回文件夹中,看看这是否能解决我当前的问题,不幸的是没有。
-
@TetsuyaYamamoto CartItem 位于 ShoppingcartApp.Classes 中,并在我的 ShoppingCartController 中使用我的命名空间中的 ShoppingcartApp.Classes 进行引用
-
有人有什么想法吗?谷歌没有太多帮助
标签: asp.net-mvc razor razor-2 razorengine