Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What controller do I put MVC layout page logic in?

Posted on 2012-03-24
6
Medium Priority
?
923 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
TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

 

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 1000 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

722 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