<p>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.</p>

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

<p>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.</p>

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

<p>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.</p>

[{'answer_id': 60561, 'author': 'Davide Vosti', 'author_id': 1812, 'author_profile': 'https://Stackoverflow.com/users/1812', 'pm_score': 4, 'selected': True, 'text': '<p>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.</p>\n\n<p>What you wanna do is quite the same thing described in <a href="http://weblogs.asp.net/stephenwalther/archive/2008/08/12/asp-net-mvc-tip-31-passing-data-to-master-pages-and-user-controls.aspx" rel="nofollow noreferrer">this</a> articles.</p>\n\n<p>I hope this helps!</p>\n\n<p>Regards</p>\n'}, {'answer_id': 66806, 'author': "Dane O'Connor", 'author_id': 1946, 'author_profile': 'https://Stackoverflow.com/users/1946', 'pm_score': 0, 'selected': False, 'text': "<p>If you don't mind strongly typed view data, you can put all the master page data in a common base class for viewData. You can set this data in the base class's constructor. All your views requiring additional data will then need strongly typed viewdata that inherits from this base class.</p>\n\n<p>To allow a call to View() in your controllers without any explicit viewdata you can override View in your ControllerBase:</p>\n\n<pre><code>protected override ViewResult View(string viewName, string masterName, object model)\n{\n    if (model == null)\n    {\n        model = new ViewDataBase();\n    }\n    return base.View(viewName, masterName, model);\n}\n</code></pre>\n"}, {'answer_id': 542968, 'author': 'Al Katawazi', 'author_id': 61962, 'author_profile': 'https://Stackoverflow.com/users/61962', 'pm_score': 1, 'selected': False, 'text': '<p>Great question. You have several options available to you.</p>\n\n<ol>\n<li>Have a jQuery call on your masterpage that grabs the data you need from a controller and then populate your fields using jQuery again.</li>\n<li>Your second option is to create user controls that make their own calls to the controller to populate their information.</li>\n</ol>\n\n<p>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. </p>\n'}]
['https://Stackoverflow.com/questions/60516', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3055/']