KWallace
KWallace

Reputation: 1698

Can I duplicate this classic ASP pattern in ASP.NET MVC?

Virtually every ASP app I've written (hundreds) follows the exact same pattern. A "single page app" with a header and footer, and a dynamically updated content area that changes depending upon what going on/in the url. Something like the following (very simplified, but demonstrates the principle):

<% select case lcase(request("action") %>
<% case "home" %>
    <div class='class_home'>
        Home Screen HTML/Script/ASP
    </div>
<% case "enroll" %>
    <div class='class_enroll'>
        Enroll Screen HTML/Script/ASP
    </div>
<% case "checkout" %>
    <div class='class_checkout'>
        <!-- if it's gonna be lengthy, will often do this instead: -->
        <!-- 
            #include file=checkout.inc.asp 
        -->
    </div>
<% end select %>

This pattern may even be nested several layers deep with additional request("subaction") subarea/subforms involved. Every form submits to itself ([form action="" method=POST]), and asp script at the top catches the form and processes it, then continues.

So, the question is, is this pattern still done inside MVC? Or do I have to duplicate the common areas over and over again in each separate page that I create?

Is this even a good idea to WANT to do this? Or is there a better way to accomplish the same goal of a "single page app"?

Thanks!

Upvotes: 0

Views: 154

Answers (3)

Rob
Rob

Reputation: 5588

I'll add a little more to the suggestions of using _ViewStart.cshtml and _Layout.cshtml. Make sure to use strongly typed view for all your Views and have each View Model extend from a base View Model class that has all the "common" data such as the menu state, logged in status, etc.

You would just do this using ineritance:

public class MyBaseViewModel
{
     public string UserName { get; set; }
     //other properties
}

public class MySampleViewModel : MyBaseViewModel
{
     //additional properties for this View only
}

Upvotes: 0

Charlie Brown
Charlie Brown

Reputation: 2825

Even in classic ASP you could achieve this without all the craziness that is going on in that select statement.

In MVC, you use partials and layout pages to avoid repeating code. Here is a nice rundown http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

Upvotes: 1

Nick
Nick

Reputation: 1122

This is still the same in MVC. If you are using Razor, look for the _Layout.cshtml file in /Views/Shared. If you are using the old ASP.Net engine, it will in the same location but called MasterPage.

Aditionally, there is a file called _ViewStart.cshtml. This is invoked automatically by the framework, and this is what points to the _Layout.cshtml file.

Upvotes: 0

Related Questions