Home MVC Pass List from DB to _Layout.cshtml
Reply: 0

MVC Pass List from DB to _Layout.cshtml

user3693281
1#
user3693281 Published in 2017-12-05 20:16:40Z

I am new to MVC and trying figure out how I would pass some data to the _Layout.cshtml view. This will be data from my database that will need to be displayed on every page.

The approach I am taking is through a partial view. Not sure if the is the best approach but it is resulting an error. Is there a better approach or how do I fix this error?

I have simplified my project to show the error I received with the partial view approach.

Error: The controller for path '/' was not found or does not implement IController.

_Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>

@{Html.RenderAction("getMenuList", "MenuView"); }


<div class="container body-content">
    @RenderBody()


@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)

MenuView.cshtml

@model System.Collections.Generic.List<PartialViewTest.Models.MenuItems>

<ul>
    @foreach (var _Menu in Model)
    {
        <li>@_Menu.LinkName</li>
    }
</ul>

MenuController.cs

namespace PartialViewTest.Controllers
{
    public class MenuController : Controller
    {

        public PartialViewResult getMenuList()
        {
            System.Collections.Generic.List<Models.MenuItems> _list;
            _list = new System.Collections.Generic.List<Models.MenuItems>
            {
                new PartialViewTest.Models.MenuItems{ LinkName = "Google", LinkURL = "http://google.com"},
                new PartialViewTest.Models.MenuItems{ LinkName = "CNN", LinkURL = "http://cnn.com"}
            };


            return PartialView("getMenuList", _list);
        }

        // GET: Menu
        public ActionResult Index()
        {
            return View();
        }
    }
}

MenuItem.cs

namespace PartialViewTest.Models
{
    public class MenuItems
    {
       public string LinkName { get; set; }
       public string LinkURL { get; set; }
    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304914 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO