Solved

What controller do I put MVC layout page logic in?

Posted on 2012-03-24
6
858 Views
Last Modified: 2012-03-26
Hi,

Disclaimer: I'm new to ASP.net MVC

I'm currently trying to convert a few websites from Web Forms over to the MVC 3 design.  I having some trouble trying to figure out where I should stick my layout (master page) logic.

I have a Master Page that contains a header, main content, right colum, footer. Currently in my Master Page code behind file in web forms makes a call to my DB and retrieves some data to be displayed in my right column.

My question is: Where does this logic go for my MVC design? Do I have to put the same right column code in every single controller I have for the site? Or, can I stick it somewhere else in one place and call it from the layout page somehow? I dont want to stick the logic in my view since that shouldn't contain any business logic.

Im trying to stick to the MVC paradigm to keep the code a clean and manageable as possible.

I've read a handful of articles about using childactions, but none of those articles tell me where I should stick these child actions.
0
Comment
Question by:maddhacker24
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:JAruchamy
ID: 37762090
Under Views/Shared folder you will find a _Layout.cshtml file... This file is similar to the master pages..

http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx
0
 

Author Comment

by:maddhacker24
ID: 37762102
I know about layout.cshtml and that is in place of my master page.

The layout file is used for layout purposes and not business logic. If I want to perform a sql query and return the results in my right column of the layout page, where would this action be performed?

If this same data needs to be returned on every controller, and every view...is there a single place this logic should be stored?

In webforms I would just build the logic into the master code behind file. However, in MVC...i'm lost as to where to build this logic so that I can have it show up in every page of my site.

Thank you, I hope this clears things up.
Kind Regards
0
 
LVL 22

Expert Comment

by:ambience
ID: 37762448
You should definitely keep the business logic outside the views. There are two simple ways that come to my mind using Html.RenderAction or Html.RenderPartial.

IF you have the data already available in the view and just want to present it like say you have List<Tags> for every Model and you want to have a single view put a Tag cloud on the page then RenderPartial is a good choice.

RenderAction is more appropriate when some business action needs to be performed but the resultant view should be displayed within the given view. Also, RenderAction gives the ability to use OutputCaching on the view that is called each time.

I found a quick example here http://www.arrangeactassert.com/when-to-use-html-renderpartial-and-html-renderaction-in-asp-net-mvc-razor-views/

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
</head>
<body>
    @RenderBody()
    @{Html.RenderAction("Show", "Category");}
</body>
</html>
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:maddhacker24
ID: 37763104
Hi,

RenderAction sounds like a good starting point.

Where would I build this action? Do I need to stick the same action in every controller I have?

In webforms I would just write the function in the master code behind. In MVC im not sure where to build this logic.

Forgive me, I'm still trying to grasp the MVC concept.
0
 
LVL 22

Accepted Solution

by:
ambience earned 250 total points
ID: 37765075
No not on every controller. RenderAction can take controller as parameter and then calls the specified action on that controller. You can have a say, CommonController, or rather put it in the most appropriate controller. For example, if the common data is a tag cloud then better have the action on TagsController.

Another way is using ActionFilters - a filter is an object whose methods are invoked right before and after every action is processed so you can factor out functionality common to all actions in an action filter, fetch data and set in the Request Context etc. This concept is explained here

http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs

and example

https://gist.github.com/1832983
0
 

Author Comment

by:maddhacker24
ID: 37766759
Thank you ambience!

For some reason I was under the impression that each controller had to correspond to a valid URL path. Knowing that I could just create a common controller that houses a bunch of actions that are used across multiple pages on my site and give them a childaction filter solves my problem. I can then call those site wide actions using RenderAction.

It's been a little tricky getting my mind to think like MVC vs web forms but now that I'm getting a handle on things I'm really enjoying the MVC architecture and the ability to control the code much more.

I appreciate your help.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now