Razor Syntax @model - @Model.Title

Mar 1, 2011 at 3:47 AM

Background: For the past 15 years I been on the sideline of web application development, mostly reading about the various platforms and tools.
I decided ASP.NET MVC was the platform - read: Vertical Cliff Face - to climb.
I am excited about MVC3 and the inclusion of Razor view syntax.
And I am thoroughly enjoying learning MVC3 with the works by studying the mvc music store.

Question: When creating the Details.cshtml store view we get this code for free:

@model MvcMusicStore.Models.Album

    ViewBag.Title = "Details";


And to call the Title Album property from the Album object, we simply use this razor syntax:

<h2>Album: @Model.Title</h2>

Why the capital M in @Model.Title and not @model.Title  ?????

This might be very intuitive for people familiar with C# and conventions used in C#.

Now, I am new to C# as well -

I have been trying to google for a primer of the Razor syntax.

MSDN does not seem to have one.

I could spend another hour googling.

Perhaps there are friendly people here that would care to respond?

borge (Newbie, intranet vb programmer trying to re-define himself!)

Mar 1, 2011 at 9:21 PM

borge - 

Glad you're enjoying the tutorial!

The @model is a directive which sets the Model type for the view. It's similar to what you'd see in a <% Page %> declaration at the top of an ASPX page. Model is a property of the page which allows accessing information in the view. They're related, but not the same thing.

There are a few primers on the Razor syntax:

The best advice I can give you on Razor is to avoid thinking of it as another programming language to be learned. Think about it as a lightweight syntax that allows you to execute code within a view, using .NET languages that have been around for a long time.

Mar 2, 2011 at 5:53 AM


Thanks for the reply, and the links to go and learn more.