Html. RenderAction of ASP. net mvc,

Source: Internet
Author: User

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
  1. <Div id = "ly_User">
  2. @ {Html. RenderAction ("LoginInfo", "Partial ");}
  3. </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
  1. <Div id = "ly_Navi">
  2. @ {Html. RenderAction ("Navi"
  3. , "Partial"
  4. , New {parentController = ViewContext. RouteData. Values ["controller"]. ToString ()});}
  5. </Div>

Send the Controller information of the current page to the user control.

[Html]View plaincopy
  1. ParentController

 

We did not declare it here.

 

In the user control VIEW _ Navi. cshtml, it is received as follows:

 

[Csharp]View plaincopy
  1. @ Model NaviModels
  2. <Div>
  3. @{
  4. StringBuilder sb = new StringBuilder ("");
  5. String controller = ViewContext. RouteData. Values ["parentController"]. ToString (). ToLower ();
  6. @ Html. Raw (sb. ToString ());
  7. }
  8. </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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.