开发者

The difference between Html.Action and Html.RenderAction

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?


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();}


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜