Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 699
Jan Carlo Viray Avatar asked Mar 28 '12 22:03

Jan Carlo Viray


People also ask

What is a view in ASP.NET MVC?

A view is an HTML template with embedded Razor markup. Razor markup is code that interacts with HTML markup to produce a webpage that's sent to the client. In ASP.NET Core MVC, views are .cshtml files that use the C# programming language in Razor markup.

What is class in ASP.NET MVC?

The model classes represents domain-specific data and business logic in the MVC application. It represents the shape of the data as public properties and business logic as methods. In the ASP.NET MVC Application, all the Model classes must be created in the Model folder.

Is a type of view in MVC?

On basis of data transfer mechanism ASP.NET MVC views are categorized as two types, Dynamic view. Strongly typed view.


2 Answers

Sure, you need to use the functions keyword in order to drop down to exposing class-level things like fields, properties, methods, and inner types:

@functions {
   public class Person
   {
       public string Name { get; set; }
   }
}

@{
   var p = new Person();
}

<span>@p.Name</span>

This will work just fine.

That being said, keep in mind that the only purpose of these inner classes is if you need to define a type only for use within a view. Myself, I've never found a need to do this for classes. However, I have taken advantage of this technique to add new methods that are not syntactically possible with helper methods.

like image 98
Kirk Woll Avatar answered Oct 12 '22 23:10

Kirk Woll


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

like image 25
James World Avatar answered Oct 12 '22 22:10

James World