View asp.net MVC中的多个ViewModel

时间:2017-02-15 07:00:36

标签: c# asp.net-mvc

我是asp.net mvc的新手,而viewmodels也是我的新手。我有一个显示产品的视图。在视图中,我想在一个div中显示所有产品。 然后在另一个div我想显示featureProducts,在另一个div中显示Toprated。我试图为每个使用不同的视图模型,因为我将在不同的页面上再次使用它们。这是正确的做法,如果不是这样的话。另外,我如何才能使用viewmodel进行练习。

域模型

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(Parent)

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 >

2 个答案:

答案 0 :(得分:0)

首先,您应该将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>

答案 1 :(得分:0)

  

我看到public int ProductId {get;组;在你的所有模特中。您的数据中似乎有一些像外键的想法,因此您可以使您的视图模型比我们在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