Fundamental Razor syntaxes

Exclusive offer: get 50% off this eBook here
Instant Razor View Engine How-to [Instant]

Instant Razor View Engine How-to [Instant] — Save 50%

Create dynamic web pages in your MVC web application with the Razor view engine with this book and ebook

$14.99    $7.50
by Abhimanyu Kumar Vatsa | June 2013 | Web Services Web Development

This article is by Abhimanyu Kumar Vatsa, the author of Instant Razor View Engine How-to, we will look at inline, code block, and mixed expressions.

(For more resources related to this topic, see here.)

Getting ready

In this view page you can try all the Razor syntaxes given in this section.

How to do it...

Here, let's start learning the fundamene written using three different approaches: inline, code block, and mixed.

Inline code expressions

Inline code expressions are always written in a single line, as follows:

I always enjoy @DateTime.Now.DayOfWeek with my family.

At runtime, the inline code expression, which is @DateTime.Now.DayOfWeek, will be converted into a day, such as Sunday. This can be seen in the following screenshot:

Let's look at one more example, which will pass the controller's ViewBag and ViewData messages on the view.

The rendered output will be as follows:

Code block expression

Code block expression is actually a set of multiple code lines that start and end with @{}. The use of opening (@{) and closing (}) characters is mandatory, even for single line of C# or VB code; as shown in the following screenshot:

This will render the following output:

Mixed code expression

Mixed code expression is a set of multiple inline code expressions in a code block where we switch between C# and HTML. The magical key here is @:, which allows writing HTML in a code block, as follows:

This will render the following output:

So, this is all about how we write the code on Razor view page.

Summary

This article thus you learned about inline code expressions, code block expressions, and mixed code expressions.

Resources for Article :


Further resources on this subject:


Instant Razor View Engine How-to [Instant] Create dynamic web pages in your MVC web application with the Razor view engine with this book and ebook
Published: May 2013
eBook Price: $14.99
See more
Select your format and quantity:

About the Author :


Abhimanyu Kumar Vatsa

Abhimanyu Kumar Vatsa works at Coxtan College, located in Bokaro Steel City, India as a lecturer focused on web technologies. He is a Microsoft MVP in ASP.NET/IIS. He loves to blog and started blogging in June 2009. He holds a bachelor's degree in Computer Science and Applications, a master's degree in Information Technology, and a few application-level diplomas. He moved to Bokaro Steel City from a remote village to pursue higher education in March 2003, and that was when he saw a computer with Windows 95 OS on Pentium 1 for the first time, and since then, he has never looked back.

Books From Packt


Microsoft Visual C++ Windows Applications by Example
Microsoft Visual C++ Windows Applications by Example

Microsoft System Center Virtual Machine Manager 2012 Cookbook
Microsoft System Center Virtual Machine Manager 2012 Cookbook

PHP and script.aculo.us Web 2.0 Application Interfaces
PHP and script.aculo.us Web 2.0 Application Interfaces

Windows Phone 7 XNA Cookbook
Windows Phone 7 XNA Cookbook

Instant Creating Data Models with PowerPivot How-to [Instant]
Instant Creating Data Models with PowerPivot How-to [Instant]

Python Geospatial Development
Python Geospatial Development

Plone 3 Multimedia
Plone 3 Multimedia

ICEfaces 1.8: Next Generation Enterprise Web Development
ICEfaces 1.8: Next Generation Enterprise Web Development


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
t
Q
t
W
Y
r
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software