i am working on my MVCOnlineShop Project , i have shown categories on homepage by creating a partial view CategoryLayout.cshtml :
@model IEnumerable<MVCOnlineShop.Models.Category> @{ ViewBag.Title = "CategoryLayout"; } <ul class="nav navbar-nav"> @foreach (var Category in Model) { <li> @Html.ActionLink(Category.CategoryName, "ProductList", new { Category = Category.CategoryName }) </li> </ul> and added this in _Layout.cshtml:
@Html.Partial("CategoryLayout") now i want to press on any category on the home page , and it will take me to products in such category , i have created this partial view ProductList.cshtml :
@model MVCOnlineShop.Models.Category @{ ViewBag.Title = "ProductList"; } <ul> @foreach (var Product in Model.Products) { <li> @Html.ActionLink(Product.ProductName, "Details", new { id = Product.CategoryID }) </li> } </ul> and this is my HomeController :
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MVCOnlineShop.Models; namespace MVCOnlineShop.Controllers { public class HomeController : Controller { OnlineStoreEntities storeDB = new OnlineStoreEntities(); // // GET: /Home/ public ActionResult Index() { var Categories = storeDB.Categories.ToList(); return View(Categories); } // // GET: /Home/Browse public ActionResult Browse(string Category) { // Retrieve Category and its Associated Products from database var CategoryModel = storeDB.Categories.Include("Products") .Single(g => g.CategoryName == Category); return View(CategoryModel); } // // GET: /Home/Details public ActionResult Details(int id) { var Product = storeDB.Products.Find(id); return View(Product); } // // GET: /Home/Browse?Category=Games public ActionResult CategoryLayout() { var Categories = storeDB.Categories.ToList(); return PartialView("CategoryLayout", Categories); } } } Question: How can i press on a category on the homepage , and this will take me to a page showing the products in this category, how can i do that?
Thanks in advance :)
ProductList.cshtml. It should be a main view where you can show the products to a particular category. And where is your action method to fetch Product for a Category ??