Html. RenderAction of ASP. net mvc,
The WEB form mode is used for development. The MVC mode is used and many things are not understood. You need to check the information at each step.
Some preliminary knowledge points are:
_ Layout. cshtml is equivalent to the master page.
The partical view (some views) is the user control.
In _ Layout. cshtml, how does one add a user control?
Assume that I have a "User Control":/Partial/LoginInfo, you can use it in _ Layout. cshtml as follows:
[Html]View plaincopy
- <Div id = "ly_User">
- @ {Html. RenderAction ("LoginInfo", "Partial ");}
- </Div>
Use Html. RenderAction.
The file structure of the user control is as follows:
The "User Control" corresponding to the red line"
If you want to transmit the vertex parameter to this user control, how can you write it?
_ Layout. cshtml:
[Html]View plaincopy
- <Div id = "ly_Navi">
- @ {Html. RenderAction ("Navi"
- , "Partial"
- , New {parentController = ViewContext. RouteData. Values ["controller"]. ToString ()});}
- </Div>
Send the Controller information of the current page to the user control.
[Html]View plaincopy
- ParentController
We did not declare it here.
In the user control VIEW _ Navi. cshtml, it is received as follows:
[Csharp]View plaincopy
- @ Model NaviModels
- <Div>
- @{
- StringBuilder sb = new StringBuilder ("");
- String controller = ViewContext. RouteData. Values ["parentController"]. ToString (). ToLower ();
- @ Html. Raw (sb. ToString ());
- }
- </Div>
For aspnet MVC3 ---- @ HtmlPartial, @ HtmlAction, @ HtmlRenderPartial, @ HtmlRenderAction
1. the return value of the Render method is void, which is output within the method. The return value type without the Render is MvcHtmlString. Therefore, you can only use the following method:
@ Html. Partial @ {Html. RenderPartial (....);}
@ Html. Action @ {Html. RenderAction (....);}
2. Html. Partial can directly provide the user control name as the parameter, while Html. Action must have the corresponding Action. In the Action, PartailResult (retun PartialView () is returned ()).
3. Html. Partial is recommended for simple user controls without any logic. Html. Action is recommended for user controls that require some Model settings. Of course, the Html. Partial method can also be used for Model data. You can refer to the method overload.
4. The advantage of using Html. Action is that you can select different user controls based on different scenarios.
For example:
@ Html. Action ("UserInfoControl ")
In the corresponding UserInfoControl Action, you can retun PartialView ("LogOnUserControl") when the user is not logged on; after logging on, You can retun PartialView ("UserInfoControl ");
Which three types of actions are available in aspnet MVC? What is the difference,
NonAction indicates that it is not a real Action, but a common method;
ChildActionOnly indicates that it can only be used in the View through Html. Action or Html. RenderAction.