39

I've been trying to figure out the difference between RenderAction and Action. I don't know if I'm so concerned about the differences at this point, as to why I can't get RenderAction to work. From what I can tell, I'm passing in the correct parameters. The overload I'm using seems to be the same for both:

@Html.RenderAction(Action, Controller, Route)

@Html.Action("Breadcrumb", "Navigation", new {SeoUrl = Model.CarlineBucket.SEOURLName})

@Html.RenderAction("Breadcrumb", "Navigation", new {SeoUrl = Model.CarlineBucket.SEOURLName})

I get a compilation error when I try and use RenderAction:

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

Any tips or hints? Should I not even be bothering with RenderAction?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
MrGrigg
  • 1,732
  • 2
  • 21
  • 26
  • 3
    Both of these methods allow you to call into an action method from a view and output the results of the action in place within the view. 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. – CD.. Apr 27 '11 at 21:11
  • possible duplicate of [Difference between Html.RenderAction and Html.Action](http://stackoverflow.com/questions/2955261/difference-between-html-renderaction-and-html-action) – GvS Apr 29 '11 at 15:06

4 Answers4

60

Try:

@{Html.RenderAction("Breadcrumb", "Navigation", new {SeoUrl = Model.CarlineBucket.SEOURLName});}

@Html.RenderAction() generates a write call to output something on the page and in your case you are not doing so because RenderAction renders the result directly to the Response.

Instead of

@Html.RenderAction()

Use

@{Html.RenderAction();}
Imdad
  • 769
  • 1
  • 11
  • 31
CD..
  • 72,281
  • 25
  • 154
  • 163
  • This did indeed do the trick. When I read that RenderAction renders the result directly to the response, I don't really understand what that means. Also, can you explain or point me where to find the difference of @{HTML()} vs @HTML() – MrGrigg Apr 28 '11 at 13:41
  • @{..} is a code block, and @.. is an Expression, have a look at - http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx – CD.. Apr 28 '11 at 16:34
  • 9
    I believe you need a semicolon at the end of the statement, just before the closing }. Otherwise razor will throw an HttpCompileException. – Joseph Daigle Apr 16 '12 at 12:12
  • There is a great video explaining PArtial vs RenderPArtial.. this will clarify the {Html ;} https://www.youtube.com/watch?v=SABg7RyjX-4 – Blue Clouds Sep 09 '17 at 08:14
18

From Phil Haack:

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.

ataddeini
  • 4,931
  • 26
  • 34
5

The return type of Html.RenderAction is void that means it directly render the responses in View where return type of Html.Action is MvcHtmlString you can catch its render view in the controller and modified it also by using following method

protected string RenderPartialViewToString(string viewName, object model)
    {
        if (string.IsNullOrEmpty(viewName))
            viewName = ControllerContext.RouteData.GetRequiredString("action");

        ViewData.Model = model;

        using (StringWriter sw = new StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
            ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
            viewResult.View.Render(viewContext, sw);
            return sw.GetStringBuilder().ToString();
        }
    }

This will return the Html string of the View.

harriyott
  • 10,505
  • 10
  • 64
  • 103
Shivkumar
  • 1,903
  • 5
  • 21
  • 32
0

Html.Action and Html.RenderAction are extension methods used as reusable portion inside parent page.

Html.RenderAction return void and it will render the result directly to the Response Stream (More efficient & fast) whereas return type of Html.Action is MvcHtmlString which can be manipulated prior to write in response stream.

Html.Action helper Syntax:
@Html.Action("Action", "Controller", new { "routeData" })

Html.Action helper Syntax:
@{ Html.RenderAction("Action", "Controller", new { "routeData" }); }

According to me, use Html.Action only if you wants to perform some modifications in string else use Html.RenderAction.