繁体   English   中英

View asp.net MVC中的多个ViewModel

[英]Multiple ViewModels in View asp.net MVC

我是asp.net mvc的新手,而viewmodels对我也是新的。 我有一个显示产品的视图。 在视图中,我想在一格中显示所有产品。 然后在另一个div中,我只想显示FeaturedProducts,在另一个div中,则显示Toprated。 我试图为每个视图使用不同的视图模型,因为我将在不同的页面上再次使用它们。 这是执行此操作的正确方法吗(如果不是)。 另外,我该如何仅针对练习使用viewmodels执行此操作。

领域模型

public class Product
{
    public int ProductId { get; set; }
    public string SKU { get; set; }
    public string Name { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public Boolean FeaturedProduct { get; set; }
    public Boolean NewProduct { get; set; }
    public Boolean TopRated { get; set; }
    public Boolean BestSellers { get; set; }
}

ViewModelProductsMain(父级)

public class ViewModelProductsMain 
{
    public ViewModelProducts Products{ get; set; }
    public ViewModelTopRated TopRated{ get; set; }
    public ViewModelFeatured Featured{ get; set; }
}

ViewModels(儿童)

public class  ViewModelProducts (Child)
//all products
{
    public int ProductId { get; set; }
    public string SKU { get; set; }
    public string Name { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
}
public class ViewModelTopRated (Child)
//all products with TopRated true
{
    public int ProductId { get; set; }
    public string SKU { get; set; }
    public string Name { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
}

public class ViewModelFeatured (Child)
//all products with Featured true
{
    public int ProductId { get; set; }
    public string SKU { get; set; }
    public string Name { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
}

调节器

public ActionResult Index()
{
    //How do I, instantiate and populate the model
    var model = new ViewModelProductsMain ();
    return View(model);
}

视图

@model IEnumerable<myProject.ViewModels.ViewModelProductsMain >

首先,您应该将ViewModelProductsMain更改为此:

public class ViewModelProductsMain
    {
        public List<ViewModelProducts> Products { get; set; }

        public List<ViewModelTopRated> TopRated { get; set; }

        public List<ViewModelFeatured> Featured { get; set; }
    }

为了在视图中显示您的产品,应编写以下代码:

@model  WebApplication1.Models.ViewModelProductsMain

@{
    ViewBag.Title = "Index";
}

<div id="AllProducts">
    @foreach (var item in Model.Products.ToList())
    {
        @item.Name
        <hr/>
    }
</div>

<div id="TopRated">
    @foreach (var item in Model.TopRated.ToList())
    {
        @item.Name
        <hr />
    }
</div>

    <div id="Featured">
        @foreach (var item in Model.TopRated.ToList())
        {
            @item.Name
            <hr />
        }
    </div>

我看到public int ProductId { 组; }在您的所有模型中。 似乎您的数据中有些像外键的想法,因此您可以使您的视图模型比我们在ViewModelProductsMain中看到的更好。

所以:

    public class Product
{
    public int ProductId { get; set; }
    public string SKU { get; set; }
    public string Name { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
}

public class ViewModelProducts {
    public virtual List<Product> Products { get; set; }
}

做您在顶部看到的ViewModelFeaturedViewModelTopRated

然后:

public class ViewModelProductsMain
{

    public ViewModelProductsMain()
    {
        this.Products = new List<ViewModelProducts>();
        this.TopRated = new List<ViewModelTopRated>();
        this.Featured = new List<ViewModelFeatured>();
    }
    public List<ViewModelProducts> Products { get; set; }

    public List<ViewModelTopRated> TopRated { get; set; }

    public List<ViewModelFeatured> Featured { get; set; }

}

不要忘了写一个构造你的ViewModelProductsMain,如果你不使构造..它带来的null reference error

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM