Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon

Fundamental Razor syntaxes

Save for later
  • 2 min read
  • 18 Jun 2013

article-image

(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:

fundamental-razor-syntaxes-img-0

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

fundamental-razor-syntaxes-img-1

The rendered output will be as follows:

fundamental-razor-syntaxes-img-2

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:

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at $19.99/month. Cancel anytime

fundamental-razor-syntaxes-img-3

This will render the following output:

fundamental-razor-syntaxes-img-4

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:

fundamental-razor-syntaxes-img-5

This will render the following output:

fundamental-razor-syntaxes-img-6

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: