- In this video I'll give you a quick overview
of Razor Syntax, and explain how you can use it
to embed C Sharp logic in your views.
For more general discussion of embedding
server side code in markup and using Razor Syntax,
independently of an MVC application,
you can take a look at my Lynda.com course,
"Up and Running with ASP dot NET",
which has a video on Razor Syntax in the second chapter.
Let's take a look at our index view again.
The point I'm starting from in this video
should match the state of the project
in the exercise files, under chapter two,
in the start folder.
Now, in general, we don't want to have
too much logic in our views,
since one of the guiding principles of MVC
is separation of concerns.
But sometimes the easiest way to dynamically display things
is with a little bit of embedded C Sharp.
So in this file, at the top,
we can see one way that Razor Syntax can be used.
We have an "at" sign with curly braces,
and inside the curly braces
we can write C Sharp as if we were inside a dot CS file.
Here we're setting a value
for the ViewBag's "title" property
using a normal C Sharp assignment statement
ending in a semicolon.
Let's look at another type of Razor Syntax.
At the bottom of the page, we'll emit some dynamic text,
say, the name of the user's browser.
When we have an "at" sign,
immediately followed by C Sharp code,
that's going to have the effect
of evaluating and rendering it as a string.
"Request dot browser" contains a lot of information
about the client's browser,
but if I just want a human-readable string name,
I can do "request dot browser dot browser".
Let's save, and take a look.
You can see that the view engine
is pretty smart about where the HTML ends
and the C sharp begins, and vice versa.
For example, it doesn't have a problem
with the comma, right after "browser", here.
It also won't get confused
by an email address, which contains an "at" sign.
A Twitter handle will break things, however.
To get around that, we can just use a double "at" sign.
Another way we can use Razor Syntax
is by creating inline helper methods
to simplify our display logic.
We do this with the "helper" key word,
which allows us to create methods that we can call
from another Razor expression, or code block.
Let's say I wanted to have some logic around
how dollar amounts for transactions are formatted.
I want negative amounts to be red,
and everything else to be green.
I could create a method called "format amount"
that takes a decimal.
Inside the method, we'll initialize color to be green,
but if the amount is less than zero,
we'll change it to red.
Then, we can simply start typing the HTML
that we want this helper method to emit.
And we can use an "at" sign
to switch back to C Sharp when necessary.
I'm going to format this as currency
for the default culture.
Then, anywhere else on the page,
even above the helper method declaration,
I can call this method.
For example, let's first create
a generic list of decimal.
This is just to have something to test it with,
and I'll give it some transaction amounts.
The integers can be explicitly cast,
or implicitly cast, as decimals,
but for things like 25, 50,
we'll want to use the m suffix
to make sure they're not treated as doubles.
So let's format these amounts,
in a list with a "for each" loop.
Again, just as easily as I can embed C Sharp in my markup,
I can put markup inside these code blocks.
Let's take another look.
We can simply refresh, no need to compile.
There's our nice formatted list of transaction amounts.
Another thing that can confuse the view engine
is plain text inside a code block.
So you can see that it's treating that as C Sharp.
Two ways around that:
We can wrap it in text tags,
or we can use an "at" sign with a colon.
You can use normal C Sharp commenting syntax
inside Razor code blocks,
but if you want to comment out
any section of the entire view file,
we can use an "at" sign, followed by an asterisk,
and then, the reverse at the end.
We'll be looking at some other special key words
that we use with Razor views very soon,
but for now, we're going to take
a closer look at layout files in the next video.