ASP. NET MVC Tour-the first stop starts with the simple razor

Source: Internet
Author: User

Remember 2011 MVC3 just out of time, we were fortunate to use MVC3 in our group purchase project, then the eldest brother let us use one weeks to familiarize with MVC, fortunately, the garden of old friend Dr is writing MvC3 series, also congratulate this

Series of articles are organized into special topics for later learning, see: http://www.cnblogs.com/highend/archive/2011/08/04/aspnet_mvc3_contents.html,2013 years into Ctrip, but also happy to see the company

upgrading WebForm to MvC3, we know that MVC has had significant changes since it was updated to 3. changes, including the addition of the Razor template engine, we all know that the razor syntax is simple and easy to start, compared to the previous ASPX

template in the grammatical simplicity has a great improvement, this article is ready to start from the razor.

A: Casually look at a few razor grammar

1. You will use "single line and code block syntax output"

1 @DateTime. now.tostring () 5 @{9     var @dt =}11 @dt  

2. You will use the "logical control statement"

1@for (inti =0; I <Ten; i++)2 {34 }5@if (DateTime.Now.Ticks/2==0)6 {7 8 }9 ElseTen { One  A}

3. You will use the "Content tag block syntax"

Use text or @: To mark the beginning of the content block.

1//The first way 2 @if (True{     } 6 {<text>) Bundle @DateTime. now.tostring () </text>13}   

4. You will use "Pass model from controller to view"

1. Controller code

1      Public classHomecontroller:controller2     {3          Publicactionresult Index ()4         {5             varStudent =NewStudent () {Name ="Ctrip", age = the };6 7             return View (student);8         }9 Ten          PublicActionResult About () One         { A             returnView (); -         } -}

2. View Code

1 @model MvcApplication1.Models.Student 2 3 4 56 

  

Well, if you know the above four, congratulations, razor this car you basically will open ... But the car does not necessarily know how to drive, the following we look at razor what is it???

Two: Anatomy razor

We know that C # is a managed language, and since it is a managed language, you need to compile C # code into the Il language and encapsulate it in a DLL ... Yes, the same thing, Razor will generate DLL after compiling ...

So here's a question, where's the DLL? What kind of organization is it???

1. Where is the DLL?

First we run the instance code, such as:

Then view the temporary files generated by the index.cshtml page:C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary asp .

See no, the PDB has already seen red, this so-called App_web_xxxx.dll is our view generated temporary DLL, next we use Ilspy to see what is inside this dll???

From the above figure, we probably see the following points:

First: Our index.cshtml is just a class (__page_views_home_index_cshtml) for the compiler ... No sense of mystery ...

Second: Our HTML tags, such as H1 's display, are only for the compiler to call the Writeliteral method ...

Third: Our class is inherited from Webviewpage<student> If you are careful, you will find that T (Student) Here is the Student entity that I plug into view.

Three: in-depth investigation

1. Webviewpage<tmodel> class

Since Webviewpage is a base class, the next question is more curious because we know that subclasses can inherit all the public methods of the parent class, right, so let's take a look at the methods in this class.

Did you see two interesting attributes, HTML and model?

1. Html

First we know that it is the htmlhelper type, usually we have two ways to write HTML tags in MVC, the first is the native HTML tag, and the other is that Webviewpage provides a variety of

HtmlHelper, isn't it interesting???

  

2. Model

Remember when we were writing the view, using @model.name to display the name, using @model.age to show the age, that Model is the attribute of the webviewpage definition, right, let's continue with

Ilspy See how @model is VALUE!!!

From which we can see that the original is Viewdata.model ... Then we look at the obj in the controller's view (object obj) is the last to whom to pass the value of ...

1          Public actionresult Index () 2         {3             varnew"ctrip"  }; 4 5            return View (student); 6         }

Through the above diagram, now we finally know ... The original view (XXX) is ultimately assigned to Viewdata.model ... In other words, I can assign a value directly to Viewdata.model in action.

Not the same??? Do as you say.

 1  public   ActionResult Index ()  2   { 3  var  student = new  student ( {Name =  ctrip  , age = 15   4  vi Ewdata.model = student;  6  re Turn   View ();  8 } 

Originally I pulled out to write is the same effect ... Well, in fact there are a lot of parents, in each Class I believe you will find a lot of familiar properties and methods, such as: Webpagebase class layout,

Page,renderbody, etc... Better details and expect you to find out.

ASP. NET MVC Tour-the first stop starts with the simple razor

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.