5

Today is my first day working with MVC and I am trying to convert my existing Web Forms website into an MVC 4 site.

I have done some reading and am starting to understand how things work but one thing I can not figure out is for the new Layouts (replacing MasterPages) where is the equivalent to the codebehind file? In my current site I have a Master Page that defines the general look and feel but also runs some code in the codebehind to changes a few things dynamically (for localization and DB generated menu system).

So now that I am using MVC and Layouts I can not figure out where I would code all that at, can anyone please point me in the right direction?

(I know MVC does not have code behinds it uses controllers for it.)

Theodoros Chatzigiannakis
  • 26,988
  • 8
  • 61
  • 97
  • 1
    This is a complicated question. Basically, there is none. Look up Partial Views with Child Actions. If you want to add `active` content to your layouts, you probably want to use Partial Views that first call Controller Actions. The Action is able to pass a model to the View and becomes a form of code behind. – Dave Alperovich Jun 09 '13 at 18:27
  • Googleing "Partial Views with Child Actions" is not returning anything that looks remotely relevant, mostly just comparisons of partival views vs partial renders. But if I am understanding you right there is no simple way of having a top level layout for my site that contains some dynamically generated content? The more I look at converting to MVC the more MVC looks to be the wrong way to go for dynamic websites. –  Jun 09 '13 at 18:50
  • kind of so-so. The child Action has similarities to code behind. I have plenty of dynamic content in my layouts. And its always created with child actions OR AJAX. – Dave Alperovich Jun 09 '13 at 18:59
  • Also, look into sections. They allow you to define in your View a section rendered in your Layout – Dave Alperovich Jun 09 '13 at 19:04
  • check out this good full length example which shows how to use 2 partial views : http://www.c-sharpcorner.com/article/html-action-and-html-renderaction-in-Asp-Net-mvc/ – Iman Aug 28 '17 at 07:54
  • another solution is to pass the model in base controller and use the model in the layout and also pass it in partialviews - https://stackoverflow.com/questions/4154407/asp-net-mvc-razor-pass-model-to-layout – Iman Aug 28 '17 at 08:31

3 Answers3

3

As you Know MVC is three layer architecture.

  1. Model
  2. View
  3. Controller

Model is the data entities. You need to store, or show the data.

Views are the html or presentation layer which would be rendered to users.

Controller are the code behind file all of your code would go in controller. It gets data from Models and apply business logic and then pass to views to show or get updated data from view and pass to models and then save to database.

_layout.cshtml file is present at path of ~/Views/Shared/_Layout.cshtml. It is master-page in mvc. You would see your partial-views contains

Layout = "~/Views/Shared/_Layout.cshtml";

this line at top of page. You can change master-page for any views and you can have multiple Layouts.

Layout contains many partial-views like left-navigation, Top-Navigation and content. each of which can be customized from controller.

Here are some links might help you:

Zaheer Ahmed
  • 26,435
  • 11
  • 70
  • 105
  • I am not looking to dynamically change layouts for views. I am looking to have a master layout that defines the entire site look and feel which will contain some dynamic items like the navigation menu and some text that needs to be localized. –  Jun 09 '13 at 18:47
  • I have update answer, but i feel you need to study some basics and start practicing. in that way you would learn more and quickly. – Zaheer Ahmed Jun 09 '13 at 18:51
  • I have been working in Web Forms for years and as I said this is day 1 of working with MVC. The reason I am trying to convert my site is so I can learn this stuff. Unfortunately for me I can't learn from just reading, I learn from seeing examples and working based off those, but I can not find any examples of what I am trying to do. I was hoping to get some friendly help on this site but it looks like that is not going to happen so I am just going to say screw MVC. The more I look into it the more it seems wrong for web applications. –  Jun 09 '13 at 19:21
3

Create a Base Controller class and make all your controllers inherit from it.

The MVC equivalent of WebForms' Master Page codebehind is then this Base Controller, where you can put code you need for multiple controllers.

How can I execute common code for every request?

Community
  • 1
  • 1
Test1
  • 31
  • 1
1

You can't find any examples of what you're trying to do, because that's not how it's done in MVC. There is no equivalent to code behinds.

You're "trying to do" the wrong thing. MVC layouts are simply template files. They have no code behind, and they should have no functionality besides simple display logic.

MVC is a different paradigm from WebForms. You don't use have server-side controls like WebForms. Therefore the idea that you have content in the layout that does it's own thing violates the MVC principles.

You're basically stuck in what's known as the XY problem. That's where you are trying to achieve certain functionality X, and you believe to do that you need to do Y, so all you ask about is Y... when X is what you really need to be asking about.

Please explain the actual thing you are trying to do, and don't assume that it must be done in the way you've always done it. For instance, if you want to localize something, then ask how to localize something. If you want dynamic content somewhere, ask how to do that, but you need to be more specific about these individual problems, and not just gloss over them as you have done here.

Erik Funkenbusch
  • 90,480
  • 27
  • 178
  • 274
  • What I am trying to do is figure out how to make a layout page that my views can run in (like a master page to content page relationship) and run c# code in. I have a main navigation menu that is generated based off data from a SQL server. The layout file for MVC needs to somehow wire up to a controller so it can execute the LINQ code to pull the data from the SQL server and assign it out to the menu system. This same functionality will need to be done on a sub-layout to control a side menu. –  Jun 09 '13 at 21:01
  • 1
    @MatthewVerstraete - MVC offers many features that can help you do this, including Razor sections, Partial Views, Actions, Razor Helpers, ajax methods, etc.. etc.. etc.. There are many methods, and what you use depends greatly on your needs, which is why you really need to learn more about these things so you can decide for yourself which one you feel is best to use. This is not a simple "Just use this" process, because MVC is much lower level than Web Forms, but gives far more flexibility and power. With that flexibility and power comes more responsibility on your part. – Erik Funkenbusch Jun 10 '13 at 01:06