Reputation: 1449
I'm using Asp.net core 2.2. In A view I want to add two models and loop through them but I dont know how to do it. This is my ProductCategory domain model
public class ProductCategory
{
public int ProductCategoryID { get; set; }
public string ProductCategoryName { get; set; }
}
And this is my Product domain model
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int ProductCategoryID{ get; set; }
[ForeignKey("ProductCategoryID")]
public virtual ProductCategory ProductCategory{ get; set; }
}
Now I want to add them in A view (or a view and multiple partial view) like this:
And as this article suggests, I don't want to use domain models in a view model/view. How do you solve this? At first I thought it's a good idea to first MyView loads ProductCategories and then different partial views load products. But in that way I should hard code model of each partial. I want to make it dynamic so when an admin adds a category and its products, the controller adds them dynamicly to MyView. If u have a problem with understanding my idea just inform me to provide u needed information as u wish. Cause I don't know what is necessary information for this post.
So When i'm designing the MyView I can do something like this (just a mockett and abstract idea of what I want to do.)
@foreach(var p in model.ProductCategory)
{
<div>
<h1>p.ProductTypeName</h1>
@foreach(var c in model.Product)
{
c.ProductName
}
</div>
}
Upvotes: 0
Views: 2079
Reputation: 14034
You need a ViewModel
like the following:
public class MyPageProductVM
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int ProductCategoryID { get; set; }
}
public class MyPageProductCategoryVM
{
public int ProductCategoryID { get; set; }
public string ProductCategoryName { get; set; }
public IList<MyPageProductVM> { get; set; }
}
Your view can now use IList<MyPageProductCategoryVM>
as the model. You could ofcourse go ahead and create another MyPageVM
class to contain the list and any additional fields, and make the view use MyPageVM
as the model.
I believe this is not what you're after, but for completeness, another option is to denormalize the domain models:
public class MyPageProductVM
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int ProductCategoryID { get; set; }
public int ProductCategoryID { get; set; }
public string ProductCategoryName { get; set; }
}
You can use a library like AutoMapper or Mapster to map the domain models with the view models.
Upvotes: 1
Reputation: 77866
I don't want to use domain models in a view model/view
Create a view model or DTO for this purpose and use the same in your view. As a good practice you should keep an isolation between your actual domain entity/model and the model(s) used for your views to display data.
public class MyViewModel
{
public IEnumerable<Product> Products {get; set;}
public ProductCategory ProductCategory {get; set;}
}
Posted example is just a simple one. You can customize this to your need (I mean the specific fields you want to display) and have only those in your view model. Not only that, now you can use DataAnnotations in your view model like
public class MyViewModel
{
[Display(Name="My Products")]
public IEnumerable<Product> Products {get; set;}
public ProductCategory ProductCategory {get; set;}
}
Upvotes: 3