asp.net-mvc RenderAction RenderPartial
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/719027/
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
RenderAction RenderPartial
提问by Ronnie
From what I have understood there is a big difference between the Html.RenderPartialincluded in the ASP.NET MVC release and the HTML.RenderActionin the Microsoft.Web.Mvc.ViewExtensionsincluded in MVC Futures.
据我所知,Html.RenderPartial包含在 ASP.NET MVC 版本中的和包含在 MVC FuturesHTML.RenderAction中的有很大的不同Microsoft.Web.Mvc.ViewExtensions。
On my application I have many pages composed from many "widgets" (sort of) each having its own specific function.
在我的应用程序中,我有许多页面由许多“小部件”(某种)组成,每个页面都有自己的特定功能。
It seemed to me more reasonable to use the RenderActionmethod as each widget would have a dedicated controller responsible for getting different data and rendering a dedicated view (as opposed to having only one controller and a unique view model to pass to RenderPartialhelper to render views).
在我看来,使用该RenderAction方法更合理,因为每个小部件都有一个专门的控制器负责获取不同的数据并呈现一个专用的视图(而不是只有一个控制器和一个独特的视图模型来传递给RenderPartial帮助程序来呈现视图)。
From the tests I have done having a form that points to a Create action method in a controller like:
根据我所做的测试,我有一个表单指向控制器中的 Create 操作方法,例如:
<% using (Html.BeginForm("Create", "Message", FormMethod.Post,
new { id = "messageCreateForm" })) {%>
and calling it with
并调用它
<% Html.RenderPartial("MessageForm",new MessageDTO()); %>
will render correcly a:
将正确呈现:
<form id="messageCreateForm" method="post" action="/Message/Create">
but with the same equivalent with RenderAction(so using a MessageFormaction method on the controller to render the view) would not render correcly so:
但与相同的等价物RenderAction(因此MessageForm在控制器上使用操作方法来呈现视图)不会正确呈现:
<% Html.RenderAction<MessageController>(m => m.MessageForm()); %>
will render in:
将呈现:
<form id="messageCreateForm" method="post" action="">
Note that the action is empty.
请注意,该操作为空。
Is this the correct way to use the RenderActionhelper and is it correct to use it in cases like that?
这是使用RenderAction助手的正确方法吗?在这种情况下使用它是否正确?
UPDATE: Actually renaming the partial view to _MessageForm renders the form correcly.
更新:实际上将局部视图重命名为 _MessageForm 会正确呈现表单。
回答by Palantir
Very old one, but it jumped into my list of unanswered questions :)
很老的,但它跳进了我未回答的问题列表:)
There is a big difference between RenderActionand RenderPartial. RenderPartialwill render a Viewon the same controller (or a shared one), while RenderActionwill actually perform an entire cycle of MVC, that is: it will instantiate the controller (any controller you mention, not just the current one), it will execute the action, and it will then return and render the result.
RenderAction和之间有很大的区别RenderPartial。RenderPartial将View在同一个控制器(或共享控制器)上渲染一个,而RenderAction实际上将执行整个 MVC 循环,即:它将实例化控制器(您提到的任何控制器,而不仅仅是当前控制器),它将执行操作,然后它将返回并呈现结果。
The RenderPartialis more similar to an inclusion, it will even share the same model if you don't specify a different one.
TheRenderPartial更类似于 an inclusion,如果您不指定不同的模型,它甚至会共享相同的模型。
The RenderActionis much more complex (and there may be undesired side effects, that's why they did not make this function available since version 1 -- initially it was available as an experimental feature).
在RenderAction更为复杂(也有可能是不希望的副作用,这就是为什么他们没有让使用此功能,因为版本1 -最初它是可以作为一个实验性的功能)。
So in your case, if you have widgets, it's OK to use both. It depends on the complexity of the widget. If you have one that has to get data from a DB, do something complex, etc... then you should probably use RenderAction.
因此,在您的情况下,如果您有小部件,则可以同时使用两者。这取决于小部件的复杂性。如果你有一个必须从数据库中获取数据,做一些复杂的事情等等......那么你应该使用RenderAction.
I have a News controller responsible for news objects. I created a Block action, which will render a block with the latest news to be put in the home page. This is a perfect example, in my opinion, for RenderAction.
我有一个负责新闻对象的新闻控制器。我创建了一个 Block 操作,它将呈现一个包含最新新闻的块,以放置在主页中。在我看来,这是 RenderAction 的一个完美示例。
回答by Pawel
Working with MVC requires much attention to not to shoot yourself in the foot. I mean it by the efficiency of the MVC products. In complex projects I would prefer to use RenderPartial rather than RenderAction. I use RenderPartial in which I use jQuery.ajax request (with Html.Action). It definitely works more efficiently than RenderAction. By this way you can put your Views into cache and then call jQuery.ajax. Try it yourselves. Ayende explains it clearly in Hibernating Rhinos.
使用 MVC 需要多加注意,不要用脚射击自己。我的意思是 MVC 产品的效率。在复杂的项目中,我更喜欢使用 RenderPartial 而不是 RenderAction。我使用 RenderPartial,其中我使用 jQuery.ajax 请求(使用 Html.Action)。它绝对比 RenderAction 更有效。通过这种方式,您可以将您的视图放入缓存中,然后调用 jQuery.ajax。自己试试吧。Ayende 在Hibernate Rhinos 中清楚地解释了这一点。

