Reputation: 797
I have an application that has products. Each product belongs to a brand and a category.
I have my models structured so when I get a category or brand, all the products within either are returned as an IEnumberable.
How do I create on partial view that I can use in either my main brand or category view that will display the products no matter what the current model is?
Thanks in advance.
Upvotes: 1
Views: 259
Reputation: 218722
Have a view which is strongly typed to List of Product
and keep it under a shared location. (~/Views/Shared
folder with a name ProductList.cshtml
)
@IList<Product>
@{
Layout=null; //assuming this is for ajax (Popups).so no layout needed.
}
@foeach(var prod in Model)
{
<p>@prod.Name</p>
<p>@prod.Price</p>
}
You can use this view to show list of Product, no matter it belongs to brand / category /whatever
public ActionResult ShowProductsForBrand(int brandId)
{
List<product> products=GetProductsForBrand(brandId);
return View("~/Views/Shared/ProductList.cshtml",products)
}
public ActionResult ShowProductsForCategory(int catId)
{
List<product> products=GetProductsForCategory(catId);
return View("~/Views/Shared/ProductList.cshtml",products)
}
Upvotes: 2