Series of articles
ASP. net mvc 3.0 Learning Series-Preface
Razor and ASP. net mvc 3.0
ASP. net mvc 3.0 Learning Series-Controllers in ASP. net mvc 3.0
ASP. net mvc 3.0 Learning Series-Model in ASP. net mvc 3.0
ASP. NET MVC
3.0 Learning Series-Dependency Resolution in ASP. net mvc 3.0
The content of this article is as follows:
1. Introduction
Razor is introduced in many articles, and many articles are introduced in the garden. We recommend that you read the ScotGu blog to see the introduction of Razor.
2. Razor's Goals
A. Easy to use & easy to learn:
Why is Razor easy to use? The first is its concise syntax, which makes it easier for us to write View pages, in addition, if you have used Razor or used it as the View Engine to publish your website or system, you will find that all the methods of Razor are separately placed in a dll, you can easily convert your current website view engine to Razor view engine. For example, you can use Razor in your asp.net webform project and use Razor syntax to generate some email sending templates. As long as you are creative, I believe you will find that Razor is easy to use. Razor is easy to learn because its syntax is quite simple. You only need to use C # And Webform View Engine, and soon you will be able to learn Razor.
B. No ties to ASP. NET runtime:
Yes, Razor does not rely on asp.net runtime. Its models are of the Dynamic type.
C. Smart:
Compared with webform view engine, Razor syntax is more smart,
3. Creating A Simpler View
Now we define a simple view and enter the following content:
At present, we do not define the variable "nicholaspei", but it can still be compiled. Running result:
An error is reported during running. The error message shows that the error message is not found in the context. Let's take a further look at how Razor runs and see the detailed error message below:
Is an exception reported by C # compilation.
Let's take a look at the following detailed code execution process:
In this line, nocholaspei is input through write, and the compiler finds that Nicolas Pei is undefined, so an exception is thrown.
So how can I output @ niclaspei?
The code above shows how to modify the View:
Run again:
It's work now ........
4. Intermingling Code and Markup
C # code and HTML code are mixed together.
I have two previous articles about Razor syntax. You can learn about it to see how to mix Razor and html tags.
Http://www.cnblogs.com/n-pei/archive/2011/01/13/1934149.html
Http://www.cnblogs.com/n-pei/archive/2011/01/15/1936267.html
This is not a long-winded question.
5. Models and ViewData
In Razor, many of them belong to the Dynamic type.
Is the base class of View and the Generic class of WebViewPage.
A.
The Model type is dynamic.
Here is an example:
Create a class Instructor first. Definition:
We use HomeController to return an IEnumerable Instructor set...
First, prepare the data:
The Controller part of the Index part:
In View, you must note that the Model is of the Dynamic type. The above code of View is as follows:
Code for the entire View:
The idea @ model is to dynamically define the Model and then use the Model in foreach.
Running result:
Note that Html. Raw () can display html tags.
B.
Added ViewBag in asp.net mvc 3, which is also a dynamic type.
Let's see what is the difference between it and ViewData:
The Controller is defined as follows:
In View, you can see the difference between ViewData and ViewBag.
In ViewDate, listColors is an object, so we need to convert it to List <string> first. In ViewBag, because ListColors is of the Dynamic type, it can be directly used.
6. HTML Helpers
Currently, all HTML Helpers can be used in Razor View Engine, such as URL Helpers and AJAX Helpers. Of course, some HTML Helpers are also added in Asp.net Mvc 3.
7. Partial Views
In the Partial View Section, a new Helper method is added in Razor: Html. Partial (ViewName, model );
First, we define a View as a javasuctor:
Modify index. cshtml and use Html. Partial ():
8. Layout Views
Layout. cshtml in Razor is equivalent to Master page in WebForm.
Other views mainly use RenderBody () and RenderSection () to specify the display position of the current View on the master.
Here you can see that there is an RenderSection ("header", required: false) This line of code. If you do not add required: false, then your header. cshtml View must exist.
9. Startup Code
Before each View rendering, The _ ViewStart. cshtml code is executed:
10. Configuration
For the configuration of Razor, you can see that it has a separate section in Web. Config to store the configuration of Razor:
If you use Razor in other web forms, remember to add this configuration.
PS: I hope you can give us valuable comments to help more people with what I write, so that you can easily understand and learn asp.net mvc 3.
Nick