The difference between Html.Action and Html.RenderAction

后端 未结 3 697
独厮守ぢ
独厮守ぢ 2020-12-09 01:49

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 ge

相关标签:
3条回答
  • 2020-12-09 02:30

    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.

    0 讨论(0)
  • 2020-12-09 02:31

    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();}
    
    0 讨论(0)
  • 2020-12-09 02:51

    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.

    0 讨论(0)
提交回复
热议问题