Model to be heavy, controller to light, view to be stupid, MVC does not want to develop the view also need to judge too much of the view-independent technology, so to keep the view logic as simple as possible. (Below is where the code appears where the curly brackets are used to prevent the word from being processed by Sina's blog)
If the view chooses the ASPX suffix,
There will be a lot of background code and HTML on the page, and the previous use of the "< angle brackets% percent%" angle brackets > "This ancient notation allows HTML tags to be mixed with. NET code chunks, so that when the page becomes complex, the readability of the mixed-class code is greatly reduced.
After MVC3, the view introduced the Razor syntax, using the commonly known small mouse "@" symbol to represent the code snippet, with the syntax highlighting vs. Razor syntax also makes the HTML tag in the entire view page and the server code a pretty good combination.
In mvc2.5, if you want to display the current time on the page:
< angle brackets%: datetime.now% percent semicolon >
If you use the razor syntax, it is
@DateTime. Now
Razor is not a code language, but a code block style that is used in view pages.
If it is C # to write the Razor page, the suffix is cshtml
If it is vb.net to write, the suffix is vbhtml
1,razor Basic Syntax
When outputting a single variable on a page, simply precede the C # statement with the @ symbol
Eg:
< tags >
Time: @DateTime. Now
</label >
Note: You do not need to end the semicolon when outputting a single variable, but you need to add a parenthesis before and after the result of a blank child or operator in the page
eg
< tags >
Member Name: @ (user. Identity.name+model.memberlevel)
Status: @ (viewbag.isenabled? ") Enable ":" Deactivate ")
</label >
When executing multiple lines of code on a page, you must add a brace before and after
eg
@
{
var a=1;
var b= "AAA";
}
Note: @{} is part of a C # code segment that must conform to the C # Language specification, which must end with a semicolon, when composing code.
If you want to insert HTML or other content in the razor syntax of a multiline background code, you must precede each line with an "@:" symbol, and in this line of code, you can add other razor variables
eg
@
{
var name= "AA";
@: Hello, I'm @name
}
The Razar view page represents a comment, using "@*" and "*@" to annotate the top and tail, not to write the example.
If you want to output the "@" symbol on the Razor page, you can use the @ symbol as the hop-off element, such as @ @aaa, output @aaa
2,razor and HTML mixed output
If statement Example
@if (viewbag.isenabled) {
@: Enabled
}else{
@: Deactivate
}
If you place a large number of HTML tags in the IF and else code block, precede each line with "@:" A bit of trouble, as long as the code block before and after a set of HTML tags can be, (plain text in the razor code will automatically be treated as C # statement, it must be labeled)
@if (viewbag.isenabled) {
< tags > Enable </tags >
}else{
< tags > Deactivate </tags >
}
The razor in the VS tool, the C # code is to show light blue, if it is a white background is razor accidental plain text, Razor will be intelligent Automatic identification is C # or HTML, but if we do not want to appear in this section of any label what to do? Use special < tag text> in Razor instead of this HTML tag, the text tag will not appear when output to the browser, of course, can also use "@:" or text as the switch between HTML and razor syntax in the multi-line C # code section.
MVC4 the difference between razor and aspx and how to use it