Best razor questions in March 2012

Is an ASP.net MVC View a "class"?

7 votes

...before everything, I'm doing this out of curiosity only. Nothing real-world application here, but just for knowledge and tinkering about...

ASP.NET Views have properties like Model and ViewData and even has methods as well.

You can even use @Using just like a regular class.cs file.

I know that it is of type WebPageView<TModel>

My main question is: is it a class?

It should be because it's a type, but..

I should be able to also do this then (Razor engine):

@{
   public class Person
   {
       //etc...
   }

   var p = new Person();
}

<span>@p.Name</span>

However I can't.. why?

note: currently a C#, ASP.net beginner.

You can't do it because Razor markup is compiled into a sequence of statements inside a method within the generated class derived from WebViewPage or WebViewPage<TModel>

The more important question though, is why would you want to do this? Instead prefer to keep Razor free of this kind of logic - it's job should be to produce layout, not do any kind of business logic, or business data transformation. Do all the heavy lifting in your action method and deliver a Model that describes the data required to render the layout in a format that requires only simple Razor markup to process.

There are quite a few tutorials a round that describe how to approach MVC and Razor. I dug up this one that is brief but does a reasonable job of covering an end-to-end story that might help you get the idea. It does include using EF to get data as well which might be more that you were bargaining for - but it's worth a read to get the full picture of how a whole architecture hangs together: http://weblogs.asp.net/shijuvarghese/archive/2011/01/06/developing-web-apps-using-asp-net-mvc-3-razor-and-ef-code-first-part-1.aspx

What c# classes and functions can be used in razor?

5 votes

Using ASP.NET MVC can be used the view engine Razor.

Razor let you use c# code after the @ sign.

As example

@if (Model.Category == "watersports")
{
    <p>Splash!!</p>
}

Furthermore you can use something like @DateTime.Now for get the current time. My question is: what functions can be used? From where Razor import them?

Any public class. They get imported with @using MyNamespace. You can also import them in the config file

<system.web.webPages.razor>

element.

ASP.net MVC View's Model vs ViewData.Model?

5 votes

I'm learning asp.net mvc and found something interesting:

It seems that I can't explicitly define a View's Model from within the View with error message saying that it has no setter.

@{ this.Model = "Hello" } //error

Then I looked at the source code in WebViewPage.cs and a View's Model property is actually like this:

public object Model { get { return ViewData.Model; } }

Thus the error.

But it's interesting how I can do this: @{ ViewData.Model = "hello"; } and actually be able to use the @model statement, resulting to "hello"

I think I'm looking too much into it, but why is this so?

beginner at C# and ASP.NET

The rule is Separation of Concern...In MVC, a Controller supplies a Model to a View and it will always be the controller that can set/assign a Model to a view....which the Views can use...this is by design...play by rules is what I would say...and If you are learning MVC its great and I would strongly recommend you to read

Stevens Sandersons MVC book