ASP. NET MVC Areas master page fetching data dynamically for rendering

Source: Internet
Author: User

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

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.