Link to home
Start Free TrialLog in
Avatar of maddhacker24
maddhacker24Flag for United States of America

asked on

What controller do I put MVC layout page logic in?

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.
Avatar of JAruchamy
JAruchamy
Flag of United States of America image

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
Avatar of maddhacker24

ASKER

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
Avatar of ambience
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>
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.
ASKER CERTIFIED SOLUTION
Avatar of ambience
ambience
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.