In razor view engine has a different type of block statements are available:-
Single Statement Block
Multiple Statement Block
Single Statement Block and Inline Expression
We define code in opening and closing curly brackets @{...} and single statement block code will be like this
@{var Series = 4 ;}
@{var RazorMessage = "Hello Razor";}
<p> MVC series : @Series</p>
<p> Razor Message : @RazorMessage</p>
Multiple Statement Block and Inline Expression
Here, in multiple statement block, we will define all variables in single curly brackets @{...} so we must need to end each variable with a semicolon and our multiple statement block code will be shown like below:-
@{
var Title = "Welcome to Razor syntax!";
var weekDay = DateTime.Now.DayOfWeek;
var HomeMessage = Title + " Today is: " + weekDay;
}
<p> Razor Message : @HomeMessage</p>
Usually, in razor engine code written at top of view will be accessible in any other code block in same view page else in case if we will declare a variable in middle and try to access a top we will not be able to access those variables and it will throw an error as it shows:-
We observe in the above code we tried to access variable "i" before declaring that is the reason it throws an error as shown above.
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In razor view engine has a different type of block statements are available:-
Single Statement Block and Inline Expression
We define code in opening and closing curly brackets @{...} and single statement block code will be like this
@{var Series = 4 ;}
@{var RazorMessage = "Hello Razor";}
<p> MVC series : @Series</p>
<p> Razor Message : @RazorMessage</p>
Multiple Statement Block and Inline Expression
Here, in multiple statement block, we will define all variables in single curly brackets @{...} so we must need to end each variable with a semicolon and our multiple statement block code will be shown like below:-
@{
var Title = "Welcome to Razor syntax!";
var weekDay = DateTime.Now.DayOfWeek;
var HomeMessage = Title + " Today is: " + weekDay;
}
<p> Razor Message : @HomeMessage</p>
Usually, in razor engine code written at top of view will be accessible in any other code block in same view page else in case if we will declare a variable in middle and try to access a top we will not be able to access those variables and it will throw an error as it shows:-
We observe in the above code we tried to access variable "i" before declaring that is the reason it throws an error as shown above.