ASP.NET MVC 3 Partial View in layout page

Viewed 34098

I'm working on setting up a shared content (navigation) for an asp.net MVC layout page.

Here is my partial view "_LayoutPartial.cshtml" with code to pull navigation data from a model.

@model MyApp.Models.ViewModel.LayoutViewModel
<p>

    @foreach (var item in Model.navHeader)
    {
        //Test dump of navigation data
        @Html.Encode(item.Name); 
        @Html.Encode(item.URL); 

    }
</p>

Here is how the code for my controller "LayoutController.cs" looks like.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyApp.Models.ViewModel;

namespace MyApp.Controllers
{
    public class LayoutController : Controller
    {

        //
        // GET: /Layout/

        LayoutViewModel layout = new LayoutViewModel();

        public ActionResult Index()
        {
            return View(layout);
        }

    }
}

Here is the code for the "_Layout.cshtml" page. I'm attempting to call the partial view here using Html.RenderAction(Action,Controller) method.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
</head>
<body>
    <p>
        @{Html.RenderAction("Index","Layout");}
    </p>

    @RenderBody()
</body>
</html>

When the layout page executes the @{Html.RenderAction("Index","Layout");} line, it throws out an error message "Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'."

What am I missing friends? How can I call a partial view in a layout page?

Thank you all in advance!

4 Answers
Related