Question

I'm trying to bring in my menu.

In my _Layout.cshtml page I have

<div class="wrapper">
                <!-- Navigation -->

                  @Html.RenderAction("Navigation", "Nav")

The Nav Controller looks like this

public ActionResult Navigation()
{
    var pages = pageRepository.Pages;
    return View(pages);
}

The Navigation View Looks like this

@model IEnumerable<Site.Domain.Entities.Page>
@{
    Layout = null;
    List<Site.Domain.Entities.Page> pages = new List<Site.Domain.Entities.Page>();

    foreach(var page in Model)
    {
        pages.Add(page);
    }
}

@foreach (var link in Model)
{
    if (link.ParentPage == "Home")
    { 
    <li>@link.PageTitle</li>
    <ul>
        @foreach (var subLink in pages)
        {
            if (subLink.ParentPage == link.PageTitle)
            { 
            <li>@subLink.PageTitle</li>
            }
        }
    </ul> 

    }
}

The view works fine when I go to .../nav/navigation

What I'm trying to do is bring this into my _Layout page so that I can use it as my menu.

I continue to get an error with @Html.RenderAction("Navigation", "Nav")

The error says "The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments"

Should I be using this as a partial? What is the best way to go about this? Thanks for any advice!

Was it helpful?

Solution

For what you're trying to do, @Html.RenderAction(..) is the correct call. RenderAction is ChildActionExtension and will need to add that attribute to the controller.

Your controller should look something like below. Note that you will want to return a PartialView as well.

[ChildActionOnly]
public ActionResult Navigation()
{
    var pages = pageRepository.Pages;
    return PartialView(pages);
}

The Render action does not return HTML, but rather adds the content to the response. With that, your view should look like:

@{@Html.RenderAction("Navigation", "Nav");}

Reference: http://msdn.microsoft.com/en-us/library/ee721274(v=vs.108).aspx

OTHER TIPS

Because Html.RenderAction is a void and does not return a value, you need to "escape" the call with braces

@{Html.RenderAction("Navigation", "Nav");}

In your controller, you should return a partial view instead.

public ActionResult Navigation()
{
    var pages = pageRepository.Pages;
    return PartialView(pages);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top