MVC 中单个视图中的多个模型 ASP.NET

Multiple models in single view in ASP.NET MVC

提问人:Dev Beginner 提问时间:5/20/2023 最后编辑:marc_sDev Beginner 更新时间:5/20/2023 访问量:43

问:

在我的 Web 应用程序 在主视图中,我想显示单独的模型数据。

首先,我创建了一个视图模型,如下所示:

public class ProductsViewModel 
{
    public IEnumerable < CakesViewModel > CakesVM { get; set; }
    public IEnumerable < PizzaViewModel > PizzaVM { get; set; }
    public IEnumerable < FastFoodsViewModel > FastFoodsVM { get; set; }
}

public class CakesViewModel 
{
    [Key]
    public int ID {get;set;}
  
    [Required]
    [Display(Name = "Product Name")]
    public string ProductName { get; set; }
   
    [Required]
    public decimal ProductPrice { get; set; }
 
    [Required]
    public string ProductPicture { get; set; }

    [Required]
    [Display(Name = "Category Name")]
    public int Category_ID { get; set; }

    public string Category_Name { get; set; }
 
    [Required]
    public bool Is_Customizable { get; set; }

    public virtual ICollection < CustomizableFoods > CustomizableFoods { get; set; }
}

public class PizzaViewModel 
{
    [Key]
    public int ID { get; set; }

    [Required]
    [Display(Name = "Product Name")]
    public string ProductName { get; set; }

    [Required]
    public decimal ProductPrice { get; set; }

    [Required]
    public string ProductPicture { get; set; }

    [Required]
    [Display(Name = "Category Name")]
    public int Category_ID { get; set; }

    public string Category_Name { get; set; }

    [Required]
    public bool Is_Customizable { get; set; }

    public virtual ICollection < CustomizableFoods > CustomizableFoods { get; set; }
}

public class FastFoodsViewModel
{
    [Key]
    public int ID { get; set; }

    [Required]
    [Display(Name = "Product Name")]
    public string ProductName { get; set; }

    [Required]
    public decimal ProductPrice { get; set; }
 
    [Required]
    public string ProductPicture { get; set; }
 
    [Required]
    [Display(Name = "Category Name")]
    public int Category_ID { get; set; }

    public string Category_Name { get; set; }

    [Required]
    public bool Is_Customizable { get; set; }

    public virtual ICollection < CustomizableFoods > CustomizableFoods { get; set; }
}

在控制器中,我尝试将单独的产品列表转换为单独的列表,并从该加载到视图模型,并将主视图模型传递给视图。

这是我的控制器代码:

List<Products> Cakes = db.Products.Where(x => x.Category_ID == 1).ToList<Products>();
List<Products> Pizzas = db.Products.Where(x => x.Category_ID == 2).ToList<Products>();
List<Products> FastFoods = db.Products.Where(x => x.Category_ID == 3).ToList<Products>();

ProductsViewModel allProducts = new ProductsViewModel();

foreach (Products item in Cakes)
{
       CakesViewModel productsView = new CakesViewModel();
       productsView.ID = item.ID;
       productsView.ProductPrice = item.ProductPrice;
       productsView.ProductName = item.ProductName;
       productsView.Is_Customizable = item.Is_Customizable;
       productsView.Category_Name = item.FoodCategories.CategoryName;
       productsView.ProductPicture = item.ProductPicture;
       allProducts.CakesVM(productsView); // here is an error
}

在“我没有看到”选项中。需要知道一个解决方案。allProducts.CakesVM(productsView);CakesVM.Add()

C# asp.net asp.net-mvc-3

评论

0赞 rene 5/20/2023
ICollection<T> 接口具有 Add 方法:learn.microsoft.com/en-us/dotnet/api/...,List of T 也实现该接口。

答:

0赞 Steve 5/20/2023 #1

第一个问题是如何为属性赋值。你不会像使用方法一样使用语法。您只需为属性赋值即可。
接下来,您的主要问题是您需要一个 CakesViewModel 列表才能为该属性分配值。没有什么是 Linq 中的一行做不到的。

var cakesVMList = Cakes.Select(item => new CakesViewModel
                  {
                      ID = item.ID;
                      ProductPrice = item.ProductPrice;
                      ProductName = item.ProductName;
                      Is_Customizable = item.Is_Customizable;
                      Category_Name = item.FoodCategories.CategoryName;
                      ProductPicture = item.ProductPicture;
                  };
allProducts.CakesVM = cakesVMList;

当然,您需要对其他属性重复此模式。