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!
RenderPartial is used to display a reusable part of within the same controller and RenderAction render an action from any controller. They both render the Html and doesn't provide a String for output.
The difference between the two is that Html. RenderAction will render the result directly to the Response (which is more efficient if the action returns a large amount of HTML) whereas Html. Action returns a string with the result.
The primary difference between the two methods is that Partial generates the HTML from the View and returns it to the View to be incorporated into the page. RenderPartial, on the other hand, doesn't return anything and, instead, adds its HTML directly to the Response object's output.
RenderPartial Returns nothing (void), it is faster than @Html. Partial, moreover requires not to create action.
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
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With