...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