asp.net-mvc 什么是 MVC 子操作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12530016/
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
What is an MVC child action?
提问by Shahrooz Jafari
I read about child actions in MVC (fundamental book), but I don't really know what it is?
我在 MVC(基础书籍)中阅读了有关子操作的信息,但我真的不知道它是什么?
Could some one please explain these methods?
有人可以解释这些方法吗?
回答by Darin Dimitrov
Phil Haack explains it nicely in this blog post. Basically a child action is a controller action that you could invoke from the view using the Html.Actionhelper:
Phil Haack 在这篇博文中很好地解释了这一点。基本上,子操作是一个控制器操作,您可以使用Html.Action帮助程序从视图中调用它:
@Html.Action("SomeActionName", "SomeController")
This action will then execute and render its output at the specified location in the view. The difference with a Partial is that a partial only includes the specified markup, there's no other action executing than the main action.
然后,此操作将在视图中的指定位置执行并呈现其输出。与 Partial 的区别在于 Partial 只包含指定的标记,除了主操作之外没有其他操作执行。
So you basically have the main action which received the request and rendered a view, but from within this view you could render multiple child actions which will go through their independent MVC lifecycle and eventually render the output. And all this will happen in the context of a single HTTP request.
因此,您基本上拥有接收请求并呈现视图的主要操作,但在此视图中,您可以呈现多个子操作,这些子操作将经历其独立的 MVC 生命周期并最终呈现输出。而这一切都将在单个 HTTP 请求的上下文中发生。
Child actions are useful for creating entire reusable widgets which could be embedded into your views and which go through their independent MVC lifecycle.
子操作对于创建可以嵌入到您的视图中并经历其独立 MVC 生命周期的整个可重用小部件很有用。
回答by Kumar Manish
A child action is an action that is invoked by using html.renderaction or html.action helper from inside of a view.
子动作是在视图内部使用 html.renderaction 或 html.action helper 调用的动作。
回答by Alexander Zaldostanov
Points to be noted:
需要注意的点:
- Any action method that is decorated with [ChildActionOnly] attribute is a child action method.
Child action methods will not respond to URL requests. If an attempt is made, a runtime error will be thrown stating - Child action is accessible only by a child request.
Child action methods can be invoked by making child request from a view using "Action()" and "RenderAction()" html helpers.
An action method doesn't need to have [ChildActionOnly] attribute to be used as a child action, but use this attribute to prevent if you want to prevent the action method from being invoked as a result of a user request.
Child actions are typically associated with partial views, although this is not compulsory.
Child action methods are different from NonAction methods, in that NonAction methods cannot be invoked using Action() or RenderAction() helpers.
Using child action methods, it is possible to cache portions of a view. This is the main advantage of child action methods.
- 任何使用 [ChildActionOnly] 属性修饰的操作方法都是子操作方法。
子操作方法不会响应 URL 请求。如果进行尝试,将抛出运行时错误,说明 - 子操作只能由子请求访问。
可以通过使用“Action()”和“RenderAction()”html helpers 从视图发出子请求来调用子操作方法。
操作方法不需要具有 [ChildActionOnly] 属性才能用作子操作,但是如果您想阻止操作方法作为用户请求的结果被调用,请使用此属性来防止。
子操作通常与部分视图相关联,尽管这不是强制性的。
子操作方法与 NonAction 方法不同,因为 NonAction 方法不能使用 Action() 或 RenderAction() 助手调用。
使用子操作方法,可以缓存视图的一部分。这是子动作方法的主要优点。
回答by yogihosting
A child action is an action method that is invoked in the view through @Html.Action().
子动作是在视图中通过@Html.Action()调用的动作方法。
Example I have an Action on my controller.
示例 我的控制器上有一个 Action。
public DateTime Time(DateTime time)
{
return time;
}
To call this action from the Viewi will use:
要从视图调用此操作,我将使用:
@Html.Action("Time", new { time = DateTime.Now })

