ASP.NET MVC 从类型 IEnumerable<cart> 渲染局部视图在类型 IEnumerable<products> 的视图中

ASP.NET MVC render partial view from type IEnumerable<cart> inside view from type IEnumerable<products>

编辑: 产品视图包含一些产品,因此我尝试使用每个产品的按钮将它们一个一个地添加到购物车中,添加的产品应出现在table 在部分视图(购物车)中 工作正常 ,我现在正在尝试在弹出模式中呈现此购物车,因此当我按下弹出按钮时它应该当我还在产品视图中时,显示我添加了哪些产品。

我正在尝试在模态正文中做这样的事情:

@Html.Partial("_ShowCart", new List<InternetApp.Models.Cart>())

但这会检索到一个空列表。

所以我想要这样的东西,但我不知道如何用不同的模型做到这一点:

@Html.Partial("_ShowCart", Model)

我有 3 个模型:产品、购物车和视图模型:

public class Product
{
    public int id { get; set; }
    public String name { get; set; }
    public float price { get; set; }
    public String image { get; set; }
    public String description { get; set; }
    public int? CategoryId { get; set; }
}

public class Cart
{
    public int product_id { get; set; }
    public DateTime added_at { get; set; }
    public virtual Product product { get; set; }
}

public class ProductCart
{
    public Product Product { get; set; }
    public Cart Cart { get; set; }
}

购物车和产品每个都有控制器,我将购物车作为局部视图,它采用 IEnumerable<Cart> 和产品视图,采用 IEnumerable<Product>

这是购物车索引操作

public ActionResult Index()
{
    List<Cart> Cart = db.Cart.Include(a => a.product).ToList();
    return PartialView("_ShowCart", Cart);
}

我不知道如何在产品中渲染购物车,因为每个产品都有不同的 IEnumerable 模型...

您的 Entity Framework (EF) 模型如下所示。我仅出于建议的标准化和清晰目的进行了修改:

public class Product
{
    [Key] //This is only needed by EF if you don't call the primary key "Id".
    public int Id { get; set; }
    public String Name { get; set; }
    public float Price { get; set; }
    public String Image { get; set; }
    public String Description { get; set; }
    //Add any additional properties.
}

public class Cart
{
    [Key]
    public int Id { get; set; }
    //Add any additional properties
}

public class CartProduct
{
    [Key]
    public int Id { get; set; }
    public int CartId {get; set; } //References Id property in Cart entity.
    public int ProductId {get; set; } //References Id property in Product entity.
    public DateTime AddedAt { get; set; }

    //These are navigation properties.  
    //The foreign key attribute lets EF know what property to reference
    [ForeignKey("CartId")]
    public virtual Cart Cart { get; set; }

    [ForeignKey("ProductId")]
    public virtual Product Product { get; set; }
}

您应该创建一组单独的模型。 EF 模型应该只用于表示您的数据库 table 和属性。将这些模型与您的 EF 模型分开放置...可能是一个 ViewModel 文件夹。

    public class CartProductsViewModel
    {
        public int CartId { get; set; } //Persist the cart you are adding to.
        public IEnumerable<ProductModel> Products {get; set; } //Available products

        public class ProductModel
        {
            public int Id { get; set; }
            public String Name { get; set; }
            public float Price { get; set; }
            public String Image { get; set; }
            public String Description { get; set; }
            //Add any additional properties.
         }

    }

现在您可以拥有一个控制器和一个视图。

public ActionResult Index(int cartId) //passing cartId for cart you are working with.
{
    var viewModel = new CartProductsViwModel();

    viewModel.CartId = cartId;
    //Get available products that you can add to cart.
    viewModel.Products = db.Products //pluralized in DbSet in EF context.
                  .Select(p => new ProductModel
                     { 
                        Id = p.Id,
                        Name p.Name,
                        Price = p.Price,
                        Image = p.Image
                        Description = p.Description
                     });

    return View(viewModel);  
}

在 return 中,我没有指定 viewModel 名称。这只有在您遵循 MVC 准则并将视图命名为与方法调用相同的名称时才有可能,在本例中为 index.cshtml。并在顶部添加以下内容:

@model CartProductsViwModel //may need to be fully qualified (add namespace).

您应该能够遍历此模型中的产品以显示在 table 中。这避免了必须处理单独的产品和购物车视图。至于将产品添加到视图,这可以在客户端完成。我建议先尝试让它工作,然后你可以创建一个专门用于客户端工作的新问题。

如果你真的需要一个对话框,你可以查找JQuery个对话框。 JQuery UI dialog

然后您需要对 post 添加的产品和 cartId 执行 ajax 调用。该方法将是这样的。

[HttpPost]
public JsonResult AddProductToCart(int cartId, ProductModel product) {}

这也是一个单独的问题。我希望这可以帮助您入门。