Solved

What controller do I put MVC layout page logic in?

Posted on 2012-03-24
6
855 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:maddhacker24
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

16 Experts available now in Live!

Get 1:1 Help Now