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

ASP.NET : MVC or WebForms ?

by Rod 21. March 2010 03:24

With Asp.net MVC 2’s launch, I’ve been more and more curious on MVC, problem is I’m a used Web forms developer.

So started doing more research on what’s best to be oriented in etc..

And found this really interesting article with SWOT analysis :

http://blog.gadodia.net/choosing-between-webforms-and-mvc/

Tags: ,

asp.net

ASP.NET : Change CSS from code behind.

by Rod 12. March 2010 13:40

A student/colleague of mine, after checking out a site I did, asked me: how do you change CSS files dynamically?

The answer is very simple;

I’m using a master page here and usually in the head container there’d be your css link;

By adding ‘runat=”server”’ and giving it a unique id you make the link run server side, and then after that modifying the ‘href’ attribute is done in the code behind.

 

<link id="masterCss" href="App_Themes/Theme1/yourcssfile.css" type="text/css" rel="stylesheet" runat="server" />

Then in the code behind :

masterCss.Href = "App_Themes/Theme1/secondcssfile.css";

Clearly that would be best set up in : “Page_Load” event or “Page_PreRender” depending on your needs :).

Tags:

asp.net

ASP.NET 4: View State

by Rod 5. March 2010 01:36

 

As web developers we’ve all had our fair share of view state, while trying to minimize them / storing them in a database or even disabling view state for some controls one by one.

When there’s more View state then we need, we lose a lot in performance. (+ not all search engines like itL)

In previous versions of asp.net, view state was enabled by default on pages therefore we had to disable view state on the controls 1 by 1.

Well guess what??

No more CTRL + C EnableViewState="false"  AND CTRL + V 1000xx times.

Now it’s possible to simply disable View state on a whole page and just enable it on the controls we want.

Here’s an example:

My page contains 1 button and 1 label, and here’s my view state :

value="/wEPDwUKLTU2ODgwMzc3NWRkf86Sf4W2JXJIMUDuy2TW5uSqft0zrNjXFhCnQ7cjfZ8="

By clicking the button (set to change the label text), the View state gets even bigger.

Now by disabling the view state on the whole page by setting ViewStateMode attribute to “Disabled” on the page directive:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ViewStateMode="Disabled" %>

 

My view state stays the same size.

As you might have expected, the label doesn’t preserve its value since its view state is also disabled, so your choice to choose which controls you need to enable view state on.

Tags: ,

asp.net

About the author

Rodrigue Hajjar

.NET Developer.

.Supinfo Student.

 

Calendar

<<  July 2010  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar