Understanding the Razor view engine in ASP. net mvc, mvcrazor
What is Razor?
Razor is a tag syntax that allows you to embed server-based code (C #) into a webpage. It is a template engine launched by Microsoft for ASP. net mvc.
C # Main Razor syntax rules
- Razor code is encapsulated in @ {...}
- In-row expressions (variables and functions) start @
- The Code statement ends with a semicolon
- String surrounded by quotation marks
- C # code is case sensitive
- C # the file extension is. cshtml.
<! -- Single-line code block -- >@{ var myMessage = "Hello World" ;}<! -- Intra-row expression or variable --> <p> The value of myMessage is: @ myMessage </p> <! -- Multiline statement code block --> @ {var greeting = "Welcome to our site! "; Var weekDay = DateTime. now. dayOfWeek; var greetingMessage = greeting + "Here in Huston it is:" + weekDay;} <p> The greeting is: @ greetingMessage </p>
Dealing with objects
The server code often involves objects.
The "Date" object is a typical ASP. NET built-in object, but you can also define an object, a webpage, a text box, a file, or a database record.
Objects can have executable methods. Database records can be saved, image objects can be rotated, email objects can be sent, and so on.
Objects can also have attributes that describe their characteristics. Database records can have the FirstName and LastName attributes.
The ASP. NET Date object has the Now attribute (written as Date. Now), and the Now attribute has the Day attribute (written as Date. Now. Day ). The following example shows how to access certain attributes of a Date object:
<table border="1"><tr><th width="100px">Name</th><td width="100px">Value</td></tr><tr><td>Day</td><td>@DateTime.Now.Day</td></tr><tr><td>Hour</td><td>@DateTime.Now.Hour</td></tr><tr><td>Minute</td><td>@DateTime.Now.Minute</td></tr><tr><td>Second</td><td>@DateTime.Now.Second</td></tr></td></table>
Read user input
Another important feature of dynamic web pages is reading user input.
The input is read by the Request [] function and tested by the IsPost condition:
@{var totalMessage = "";if(IsPost) { var num1 = Request["text1"]; var num2 = Request["text2"]; var total = num1.AsInt() + num2.AsInt(); totalMessage = "Total = " + total; }}Convert Data Types
It is sometimes useful to convert one data type to another.
Most common examples are to convert string input to another type, such as integer or date.
As a rule, user input becomes a string, even if the user inputs a number. Therefore, the numeric input value must be converted to a number before being used for calculation.
The following is a list of common conversion methods:
Method |
Description |
Instance |
|
Converts a string to an integer. |
If (myString. IsInt () {myInt = myString. AsInt ();} |
|
Converts a string to a floating point number. |
If (myString. IsFloat () {myFloat = myString. AsFloat ();} |
- AsDecimal ()
- IsDecimal ()
|
Converts a string to a decimal number. |
If (myString. IsDecimal () {myDec = myString. AsDecimal ();} |
- AsDateTime ()
- IsDateTime ()
|
Convert string to ASP. NET DateTime type |
MyString = "10/10/2012"; myDate = myString. AsDateTime (); |
|
Converts a string to a logical value. |
MyString = "True"; myBool = myString. AsBool (); |
ToString () |
Converts any data type to a string. |
MyInt = 1234; myString = myInt. ToString (); |