- Use a single variable directly@aThe form of does not require a semicolon, generally use existing variables directly, pay attention to usinghtmlWhen tagging, razorAdd a space to the expression.
//The following are all wrong @int a=0 //There can't be spaced//@int a=0, @int will be recognized as a Razor expression, a=0 will be recognized as a character, and @int will report an error @int a = 0//No spaces<p>How many people are there@a</a> //It will not be recognized here, it has become a character<h1 class="display-4">Welcome @a;</h1> // The input is@a; Will only recognize@a,and;Will become a character
- Razor code is encapsulated in @{ ... } --Multiple lines of code need to be included in @{}middle
@{ int a = 0} //Report an error,exist@{}Press normallyC#Write code,Add a point number after the statement
- Encapsulated in@{ ... }In , Razor's variables are global, and a variable is declared at one position and can be used later.
- If the variable isif/for{} If declared at et al, it is a local variable. Forif/for For these functions, they can be not included in @{} and can be used directly@if/@forThis form
<!-- Multi-line statement code block 1 --> @{ var greeting = "Welcome to our site!"; var weekDay = ; var greetingMessage = greeting + " Here in Huston it is: " + weekDay; } <!-- Multi-line statement code block 2 --> <ul> @for (int i = 0; i < 100; i++) { i += 1; <li>@i</li> }
- In-line expressions (variables and functions) start with @ --A single expression such as assignment or output, you only need to add @ at the beginning
<!-- Single line of code block --> @{ var myMessage = "Hello World"; } <!-- In-line expressions or variables --> <p>The value of myMessage is: @myMessage</p>
- Code statements end with semicolons
- Strings are surrounded by quotes
- C# code is case sensitive, Razor is the same
hint:
The variables declared in @{} are global.
@var net = "From the beginning to the abandonment"; is the wrong way to write it, because single-line expressions cannot have spaces. Please use @{ var net = "From Getting Started to Give Up";}
Razor code can be placed anywhere in the HTML tag, combined with the tag to present content to the user.
In the multi-line code block @{}, if there are only c# code statements, there is no need to prepend each statement, such as the first item in the knowledge summary.
The multi-line statement block @{} can contain HTML tags. If you need to use code in HTML tags, you only need to add a statement in the format of "@ statement" to a certain part.
In the example "Multi-line statement code block 2", "@for{}" is directly used, which contains multiple lines of code. Here, multiple lines of code can be added to the for function "for{}", which is consistent with "@{}".
In addition to the for function, if , if...else... , while and other functions can also contain multiple lines of code.
More complex situations:
<div class="container-fluid col-10"> @{ if (Model != null) { if ( == 200) { var a = (AccurateBasicModel); if ( != null) { <p>Image orientation @</p> } <p>Number of identified text @a.words_result_num</p> int i = 1; <table class="table table-hover table-responsive table-bordered"> <tr><td>serial number</td><td>Identify text</td><td>Row confidence average</td><td>Row confidence variance</td><td>Minimum value of confidence</td></tr> @foreach (var item in a.words_result) { <tr> <td>@i</td> <td> @</td> @{ if ( != null) { <td>@</td> <td>@</td> <td>@</td> } else { <td>Not set</td> <td>Not set</td> <td>Not set</td> } } @{ i++;} </tr> } </table> } else { <h3>Identification errors,Error details:</h3> <p> @() </p> } } else { <h2 >Please upload pictures</h2> } } </div> </div>
This is all about this article about C# Razor grammar rules. I hope it will be helpful to everyone's learning and I hope everyone will support me more.