ASP.NET MVC 3 Preview 1 : New Features Part 1

by Rod
29. July 2010 00:05

This is isn't an introduction post on MVC so if you’re looking through to understand what MVC is I suggest you binging/googling it!

ASP.NET MVC 3 Preview 1 was released yesterday, this post is the first among other posts I’m going to write that will walkthrough some of the new features.

You can get the bits here.

Phil Haack wrote an article with a detailed summary of what’s new (or even better read the release notes ;)).
In short here are the new features:
* Razor Syntax View Engine.
* Dynamic View and ViewModel propeties.
* Service Location and Dependency Injection Support.
* Global Filters.
* Support for .NET Framework 4 Metadata Attributes.

Now let’s get into the practical:
After having installed the bits, Open up a new project and choose “ASP.NET MVC 3 Web Application (Razor)”
You might of noticed 2 Templates are available one with Razor View engine and the other ASPX, this will be regrouped later on in one template, and you’ll make your choice later on in the project creation (probably the same time you choose whether or not you want to create a Unit test project)

After creating the project scroll down to Views/Shared, you’ll notice the _Layout.cshtml page which is the equivalent of the Master Page in ASPX.
Like in Master Pages, you can define different sections in your “_Layout.cshtml” file.

We’re going to define add some custom content in header (we’re going to add a slogan) and a footer.
In order to do that we’ll use @RenderSection and end up with something like this:

 

layout

 

This release doesn’t support syntax highlighting or intellisense, so you’ll understand why the screenshots might look plain!

Now what we want to do is define what’s going to be rendered in these sections through our Index.cshtml (home view); we’ll add the content to the sections with “@section nameofthesection ”

home

 

Everything outside @section is rendered as normal body content.

So with this we’ll end up with something like this :

homeresult

Now let’s get into something the Razor View Engine made easier and faster for us:

Usually when you’d want to write c# code and iterate through a list in the VIEW you’d do something like this :

mvc2iteration

 

As you might of noticed we’re really bound to this <$= %> syntax!

In MVC 3 the mixing is a lot smoother (This is proper Razor syntax):

 

mvc3iteration

 

 

You might of noticed in the home view, there’s something weird and new View.Message”, although there’s a property called “View” in the home controller, so where did this come from?

As you’ve already guessed @View.Message is a proper cleaner way to write “ViewData["Message"]”.
Now let’s get to the controller you can see :

ViewModel.Message = "Welcome to ASP.NET MVC!";
//This also is the equivalent to :
ViewData["Message"] = "Welcome to ASP.NET MVC!";

So why not call it View here on the controller? Because View is already a method; the Microsoft team found it shorter and cleaner than ViewModel on the View but they’re still open for suggestions for a smoother short name.

So this concludes this post; There are more new interesting features, but will leave them for future posts!

 

 

 

Tags: , ,

asp.net

About the author

Rodrigue Hajjar

.NET Developer.

.Supinfo Student.

 

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar