.net Html.Action 和 Html.RenderAction 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5810598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The difference between Html.Action and Html.RenderAction
提问by MrGrigg
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:
我一直在试图找出 RenderAction 和 Action 之间的区别。我不知道在这一点上我是否如此关心这些差异,至于为什么我不能让 RenderAction 工作。据我所知,我正在传递正确的参数。我使用的重载似乎对两者都相同:
@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:
当我尝试使用 RenderAction 时出现编译错误:
CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments.
CS1502:“System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)”的最佳重载方法匹配有一些无效参数。
Any tips or hints? Should I not even be bothering with RenderAction?
任何提示或提示?我什至不应该为 RenderAction 烦恼吗?
回答by CD..
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 RenderActionrenders the result directly to the Response.
@Html.RenderAction()生成写入调用以在页面上输出某些内容,在您的情况下,您没有这样做,因为RenderAction将结果直接呈现给响应。
Instead of
代替
@Html.RenderAction()
Use
用
@{Html.RenderAction();}
回答by ataddeini
From Phil Haack:
来自菲尔哈克:
The difference between the two is that
Html.RenderActionwill render the result directly to the Response (which is more efficient if the action returns a large amount of HTML) whereasHtml.Actionreturns a string with the result.
两者之间的区别在于
Html.RenderAction将结果直接呈现给响应(如果操作返回大量 HTML,则效率更高),而Html.Action返回带有结果的字符串。
回答by Shivkumar
The return type of Html.RenderActionis voidthat means it directly render the responses in View where return type of Html.Action is MvcHtmlStringyou can catch its render view in the controller and modified it also by using following method
的返回类型Html.RenderAction是void指它直接在视图中呈现响应,其中 Html.Action 的返回类型是 MvcHtmlString您可以在控制器中捕获其呈现视图并使用以下方法对其进行修改
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 字符串。

