Solved

What controller do I put MVC layout page logic in?

Posted on 2012-03-24
6
869 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How our DevOps Teams Maximize Uptime

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

 

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net creating Contact in Outlook 1 59
WPF user control poped up from a window 1 35
Calculating percentage 2 32
Display info from DB to Label in asp.net 7 18
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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