简体   繁体   English

View asp.net MVC中的多个ViewModel

[英]Multiple ViewModels in View asp.net MVC

I am new to asp.net mvc and viewmodels are new to me as well. 我是asp.net mvc的新手,而viewmodels对我也是新的。 I have a view that shows products. 我有一个显示产品的视图。 In the view I want to show all the Products in one div. 在视图中,我想在一格中显示所有产品。 then in another div I want to show just the featuredProducts and in another div the Toprated. 然后在另一个div中,我只想显示FeaturedProducts,在另一个div中,则显示Toprated。 I am trying to use different viewmodels for each because I am going to use these again on different page. 我试图为每个视图使用不同的视图模型,因为我将在不同的页面上再次使用它们。 Is this the correct way to go about doing this and if not what is. 这是执行此操作的正确方法吗(如果不是)。 Also how can I do this with viewmodels just for the exercise. 另外,我该如何仅针对练习使用viewmodels执行此操作。

Domain Model 领域模型

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) ViewModelProductsMain(父级)

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

ViewModels (children) 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; }
}

Controller 调节器

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

View 视图

@model IEnumerable<myProject.ViewModels.ViewModelProductsMain >

First you should change ViewModelProductsMain to this: 首先,您应该将ViewModelProductsMain更改为此:

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

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

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

and for display your product in your view should write this code: 为了在视图中显示您的产品,应编写以下代码:

@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>

i see public int ProductId { get; 我看到public int ProductId { set; 组; } in all of your models. }在您的所有模型中。 seems you have some think like Foreign Keys in your data so you can make your view model better than what we see in ViewModelProductsMain. 似乎您的数据中有些像外键的想法,因此您可以使您的视图模型比我们在ViewModelProductsMain中看到的更好。

so: 所以:

    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; }
}

do what you see at top to ViewModelFeatured and ViewModelTopRated to 做您在顶部看到的ViewModelFeaturedViewModelTopRated

then: 然后:

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; }

} }

don't forget to write a constructor for your ViewModelProductsMain if you dont make constructor ..it brings null reference error 不要忘了写一个构造你的ViewModelProductsMain,如果你不使构造..它带来的null reference error

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

相关问题 如何在ASP.NET MVC 4中保存多个ViewModel? - How can I save multiple ViewModels in ASP.NET MVC 4? ASP.NET MVC中的ViewModel和域模型 - ViewModels and domain models in ASP.NET MVC 如何在ASP.NET MVC中使用ViewModels? - How to use ViewModels in ASP.NET MVC? ASP.NET MVC-如何在C#中构建自定义视图模型以将数据集传递给强类型视图? - ASP.NET MVC - How to build custom viewmodels in C# to pass a data set to a strongly typed View? ASP.Net MVC C#另一个viewmodel中的两个viewmodel - 如何在视图中引用 - ASP.Net MVC C# two viewmodels within another viewmodel - how to reference in the view ASP.Net MVC在View中的多重继承 - ASP.Net MVC multiple inheritance in a View 使用多个模型比使用ASP.NET MVC中的ViewModel有更好的方法吗? - Is there a better way to consume multiple Models than through ViewModels in ASP.NET MVC? 如何在 ASP.NET CORE MVC 应用程序中为多个表创建 ViewModel? - How to create ViewModels in ASP.NET CORE MVC application for multiple tables? ASP.NET MVC:使用多个viewModel,以及如何将数据正确传递给模型? - ASP.NET MVC: Using multiple viewModels and how to pass the data correct to the model? Asp.net 核心 MVC 中 ViewModel 的适配器模式 - Adapter Pattern for ViewModels in Asp.net core MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM