[英]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 做您在顶部看到的
ViewModelFeatured
和ViewModelTopRated
到
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.