This article is currently in the process of being translated into Vietnamese (~31% done).
Razor:
Variables
Giống C#, bạn có thể định nghĩa biến trong Razor, để lưu thông tin cho việc sử dụng lại. Nếu trong code thì có thể dùng luôn và nếu trong Razor thì có thể định nghĩa biến bên trong. Ví dụ:
@{
string helloWorldMsg = "Hello, world!";
}
You can of course output it just as easily, either directly in the code block or outside of it, by referencing the name. Here's an example of it:
@{
string helloWorldMsg = "Hello, world!";
}
<div>
@helloWorldMsg
</div>
You can of course work with and manipulate your variables and apply logic to them, just like you would in C#:
@{
string helloWorldMsg = "Good day";
if(DateTime.Now.Hour > 17)
{
helloWorldMsg = "Good evening";
}
helloWorldMsg += ", world!";
helloWorldMsg = helloWorldMsg.ToUpper();
}
<div>
@helloWorldMsg
</div>
Summary
Declaring and using variables in Razor is just as easy as using them in your regular C# code. As you will see in later examples, it can be really powerful to have easy-access to variables in your markup.