Search code examples
c#asp.net-mvc-3viewdatarazor

ASP.NET MVC 3 RC - Razor "View" Property


Just mucking around with Razor in the ASP.NET MVC 3 RC released today.

Now, we have a concept of a "Layout Page", which i presume is the replacement of the "View Master" in the ASPX view engine.

But i do not understand the "View" property of the layout page.

Here is the example which is created when you create a new Razor View:

_Layout.cshtml

<html>
<head>
   <title>@View.Title</title>

...

MyView.cshtml

@model Mvc3FunParty.Models.Post

@{
   View.Title = "Some Title";
   Layout = "~/Views/Shared/_Layout.cshtml";
}

Which results in "Some Title" being inserted into the <title> tag of the rendered HTML.

How on earth does this work? When i hover over the "View" property, it's of type "dynamic".

So what exactly should this property be used for? Can we stuff anything in there? Is this supposed to be the Razor implementation of ViewData?

And if so, shouldn't it be of type "ViewDataDictionary"? Why the "dynamic" type for the View property?


Solution

  • Yes, "View" is really ViewData and they're using dynamic so as to have the syntax you're seeing (View.Title)

    It translates to

    ViewData["Title"]
    

    Lots of little nuggets like this in MVC and specially in MVC 3 that will confuse you :).