Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

should I be using an @html.renderpartial or @html.renderaction

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!

like image 326
haydnD Avatar asked Jan 25 '13 16:01

haydnD


People also ask

What is the difference between RenderAction and RenderPartial?

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.

What is the difference between HTML action and HTML RenderAction?

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.

Whats the difference between HTML partial and HTML RenderPartial?

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.

What are the benefits of HTML dot RenderPartial over?

RenderPartial Returns nothing (void), it is faster than @Html. Partial, moreover requires not to create action.


2 Answers

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

like image 168
anAgent Avatar answered Sep 28 '22 15:09

anAgent


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);
}
like image 33
David L Avatar answered Sep 28 '22 13:09

David L