Original: ASP. NET MVC Tour-the first stop starts with the simple razor
Remember 2011 MVC3 just out of time, we were fortunate to use MVC3 in our group purchase project, then the eldest brother let us spend one weeks to familiarize with MVC, fortunately, the old friend in the garden Dr
are Write MVC3 Department columns, congratulations on this, too . series of articles are organized into special topics for later learners, see: Http://www.cnblogs.com/highend/archive/2011/08/04/aspnet_mvc3_
contents.html,2013 years in Ctrip's time, also happy to see the company upgrading WebForm to MvC3, we know that MVC has had significant changes since it was updated to 3. of Change,
which includes the new Razor template engine, Big Homes are knowing 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 {<tex T> end @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 ... Right, the same thing, Razor will generate after compiling
Dll... 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 see this DLL inside exactly
What do you have???
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, and then we'll look at the next step.
See what the methods are 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 Webviewpage
The various htmlhelper provided, is not very interesting???
2. Model
Remember when we wrote the view, we used @model.name to display names and @model.age to show age, and that Model was the attribute of webviewpage definition, right?
Below we continue to use Ilspy to see how @model is valued!!!
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 give viewdata.mode directly in action
L assignment is 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