How to Render the categories in database on SQL server in _Layout.cshtml

Viewed 287

i am trying to show the categories in my database on SQL on the homepage of my MVC web , how to do that? I have this in my _Layout.cshtml:

<div class="list-group">
                    <a href="#" class="list-group-item">Category 1</a>
                    <a href="#" class="list-group-item">Category 2</a>
                    <a href="#" class="list-group-item">Category 3</a>
                </div>

this is my Views/store/index.cshtml:

@model IEnumerable<MVCOnlineShop.Models.Category>

@{
    ViewBag.Title = "Store";
}
<h3>Browse Categories</h3>
<p>
    Select from @Model.Count()
    Categories:
</p>
<ul>
    @foreach (var Category in Model)
    {

        <li>
            @Html.ActionLink(Category.CategoryName,
"Browse", new { Category = Category.CategoryName })
    </li>
    }
</ul>

this is my StoreController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCOnlineShop.Models;

namespace MVCOnlineShop.Controllers
{
    public class StoreController : Controller
    {
        OnlineStoreEntities storeDB = new OnlineStoreEntities();
        //
        // GET: /Store/

        public ActionResult Index()
        {
            var Categories = storeDB.Categories.ToList();
            return View(Categories);
        }
        //
        // GET: /Store/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: /Store/Details
        public ActionResult Details(int id)
        {
            var Product = storeDB.Products.Find(id);

            return View(Product);
        }
        //
        // GET: /Store/Browse?Category=Games

    }
}

and this is my Global.asax:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace MVCOnlineShop
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {


            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }
    }
}
2 Answers
Related