的RenderAction的RenderPartial [英] RenderAction RenderPartial

查看:109
本文介绍了的RenderAction的RenderPartial的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我知道有很大区别的 Html.RenderPartial 包含在ASP.NET MVC的释放和 HTML.RenderAction Microsoft.Web.Mvc.ViewExtensions 包含在MVC期货。

From what I have understood there is a big difference between the Html.RenderPartial included in the ASP.NET MVC release and the HTML.RenderAction in the Microsoft.Web.Mvc.ViewExtensions included in MVC Futures.

在我的应用程序有许多部件由许多页面(在某种程度上)各有其特定的功能。

On my application I have many pages composed from many "widgets" (sort of) each having its own specific function.

在我看来,更合理的使用的RenderAction 方法,每个插件会对负责获取不同的数据,呈现一个专用的视图(而不是有一个专用控制器只有一个控制器和一个独特的视图模型传递到的RenderPartial 助手来渲染视图)。

It seemed to me more reasonable to use the RenderAction method 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 RenderPartial helper to render views).

这是我做了具有指向创建操作方法在控制器形式的测试,如:

From the tests I have done having a form that points to a Create action method in a controller like:

 <% using (Html.BeginForm("Create", "Message", FormMethod.Post, 
     new { id = "messageCreateForm" })) {%>

和与调用它

 <% Html.RenderPartial("MessageForm",new MessageDTO()); %>

将正确地将一个渲染:

will render correcly a:

<form id="messageCreateForm" method="post" action="/Message/Create">

但具有相同的等效采用的RenderAction (所以使用控制器上的 MessageForm 操作方法渲染视图)不会使正确地将这样:

but with the same equivalent with RenderAction (so using a MessageForm action method on the controller to render the view) would not render correcly so:

 <% Html.RenderAction<MessageController>(m => m.MessageForm()); %>

将呈现:

<form id="messageCreateForm" method="post" action="">

请注意该操作是空的。

这是使用的RenderAction 帮手正确的方法和它正确地使用它的情况下这样?

Is this the correct way to use the RenderAction helper and is it correct to use it in cases like that?

更新:
其实重命名局部视图_MessageForm呈现形式正确地将

UPDATE: Actually renaming the partial view to _MessageForm renders the form correcly.

推荐答案

很老的一个,但它跳入我的悬而未决的问题清单:)

Very old one, but it jumped into my list of unanswered questions :)

的RenderAction 的RenderPartial 之间有很大的区别。 的RenderPartial 将呈现的 查看 的同一个控制器(或共享的)上,而的RenderAction 将实际执行MVC的整个周期,即:它将实例控制器(你提到任何控制器,而不仅仅是当前的),它会执行动作,它将然后返回并呈现结果。

There is a big difference between RenderAction and RenderPartial. RenderPartial will render a View on the same controller (or a shared one), while RenderAction will 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.

的RenderPartial 更类似于的包容,如果你没有它甚至会共用同一个模型指定一个不同的。

The RenderPartial is more similar to an inclusion, it will even share the same model if you don't specify a different one.

的RenderAction 要复杂得多(也有可能是不希望的副作用,这就是为什么他们不提供这些功能,因为版本1 - 它最初是作为一个实验性功能)。

The RenderAction is 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).

所以你的情况,如果你有小部件,它的确定同时使用。这取决于小部件的复杂性。如果你有一个有获得来自DB数据,做复杂的东西,等等,那么你应该使用的RenderAction

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.

这篇关于的RenderAction的RenderPartial的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆