Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put master page's code in an MVC application?

I'm using a few (2 or 3) master pages in my ASP.NET MVC application and they must each display bits of information from the database. Such as a list of sponsors, current fundings status etc.

So my question was, where should I put these master-page database calling code?

Normally, these should goes into its own controller class right? But then that'd mean I'd have to wire them up manually (e.g. passing ViewDatas) since it is out of the normal routing framework provided by the MVC framework.

Is there a way to this cleanly without wiring ViewData passing/Action calls to master pages manually or subclassing the frameworks'?

The amount of documentation is very low... and I'm very new to all this including the concepts of MVC itself so please share your tips/techniques on this.

like image 247
chakrit Avatar asked Sep 13 '08 13:09

chakrit


People also ask

Can we use master page in MVC?

You add a new view master page to an MVC project by right-clicking the Views\Shared folder, selecting the menu option Add, New Item, and selecting the MVC View Master Page template (see Figure 1).

How do you apply master page on application level?

You can attach content pages to a master page at three levels: At the page level You can use a page directive in each content page to bind it to a master page, as in the following code example. At the application level By making a setting in the pages element of the application's configuration file (Web.

What is the location of master page of MVC core web application?

Open your Solution Explorer, and go to Views Folder, open Home Folder, there are About. cshtml , Contact. cshtml and Index. cshtml files.

How do I add a master page?

Add the master page into our project. Right click Project->Add->New item (shown in the picture), After clicking on new item, Window will open, select Web Form->Web Forms Master Page (shown in the picture), After clicking the add button, master page 'site1.


2 Answers

One way to do this is to put in the masterpage view the hook for the ViewData and then you define a BaseController : Controller (or multiple base classes) where you do all the db calls you need.

What you wanna do is quite the same thing described in this articles.

I hope this helps!

Regards

like image 153
Davide Vosti Avatar answered Sep 24 '22 16:09

Davide Vosti


Great question. You have several options available to you.

  1. Have a jQuery call on your masterpage that grabs the data you need from a controller and then populate your fields using jQuery again.
  2. Your second option is to create user controls that make their own calls to the controller to populate their information.

I think the best choice is creating controls for the region of your masterpage that has data that needs to be populated. Thus leaving your masterpage to strictly contain design elements. Good luck.

like image 42
Al Katawazi Avatar answered Sep 22 '22 16:09

Al Katawazi