Html.renderaction of ASP. NET MVC

Source: Internet
Author: User

Web Forms mode development accustomed to, cut into the MVC pattern, a lot of things do not understand, every step to look for information.

Some of the initial points of knowledge learned are:

_layout.cshtml is the equivalent of a master page

Then the partical view (partial view) is the user control.

So how do you want to put a user control in _layout.cshtml?

Suppose I have a "user control":/partial/logininfo, you can use this in _layout.cshtml:

[HTML]View Plaincopy
    1. <div id="Ly_user">
    2. @{html.renderaction ("Logininfo", "Partial");}
    3. </div>

With Html.renderaction.

The file structure for the user control is as follows:

The red line is the corresponding "User control"

If you want to pass a point parameter to this user control, how to 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>

Pass the controller information of the current page to the user control, note that this

[HTML]View Plaincopy
    1. Parentcontroller

It is our own freedom here, without prior notice.

In the user control view, _navi.cshtml receives:

[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>

Html.renderaction of ASP. NET MVC

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.