It is often necessary to pull out some common page elements and make them into master pages, but the elements here are generally basic elements, i.e. no need
Basic data for background data exchange, but for some data that needs to be queried through the background, how should we pass it to the foreground master page?
This describes a unified data section for multiple pages, a page that is consistent across multiple pages and has no other relationship to each page.
While we can use the C # language to get data and write to the page through razor, this violates our view's willingness to handle data as little as possible.
So the solution is to extract the data that is read into the Basecontroller, by writing the required ViewBag to the page,
Then other controls inherit our custom controllers,Basecontroller need to inherit controller, and need to import using SYSTEM.WEB.MVC;
The code is as follows
Basecontroller. cs
Public class Basecontroller:controller { public Basecontroller () { // Pass in the data we need " test Data " } }
Inherit the custom Basecontroller
Public class Homecontroller:basecontroller { public actionresult Index () { return View (); } }
Extract the public part of the page as _main.cshtml saved to the shared directory
Write @viewbag.clubs or other required data on the master page,
Then add content such as @renderbody () to render other parts of the page
At this point, the last step is to add a reference to the master page in the corresponding view page
@{ "~/areas/student/views/shared/_main.cshtml";}
2017.12.9 20:16
Record it for later and make it easy for others
ASP. NET MVC Areas master page fetching data dynamically for rendering