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
- <div id="Ly_user">
- @{html.renderaction ("Logininfo", "Partial");}
- </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
- <div id="Ly_navi">
- @{html.renderaction ("Navi"
- , "Partial"
- , new { Parentcontroller = viewcontext.routedata.values["Controller"]. ToString ()});}
- </div>
Pass the controller information of the current page to the user control, note that this
[HTML]View Plaincopy
- Parentcontroller
It is our own freedom here, without prior notice.
In the user control view, _navi.cshtml receives:
[CSharp]View Plaincopy
- @model Navimodels
- <div>
- @{
- StringBuilder sb = new StringBuilder ("");
- string controller = viewcontext.routedata.values["Parentcontroller"]. ToString (). ToLower ();
- @Html. Raw (sb.) ToString ());
- }
- </div>
Html.renderaction of ASP. NET MVC