Part 57 Razor views in mvc

Link for code samples used in the demo
http://csharp-video-tutorials.blogspot.com/2013/07/part-57-razor-views-in-mvc.html

Link for csharp, asp.net, ado.net, dotnet basics, mvc and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat/playlists

Make sure to replace [ with LESSTHAN and ] with GREATERTHAN symbol.

In this video, we will discuss razor view syntax.

Use @ symbol to switch between c# code and html.
@for (int i = 1; i [= 10; i++)
{
[b]@i[/b]
}

Output:
1 2 3 4 5 6 7 8 9 10

Use @{ } to define a code block. If we want to define some variables and perform calculations, then use code block. The following code block defines 2 variables and computes the sum of first 10 even and odd numbers.
@{
int SumOfEvenNumbers = 0;
int SumOfOddNumbers = 0;

for(int i =1; i[=10; i++)
{
if(i %2 == 0)
{
SumOfEvenNumbers = SumOfEvenNumbers + i;
}
else
{
SumOfOddNumbers = SumOfOddNumbers + i;
}
}
}

[h3]Sum of Even Numbers = @SumOfEvenNumbers[/h3]
[h3]Sum of Odd Numbers = @SumOfOddNumbers[/h3]

Output:
Sum of Even Numbers = 30
Sum of Odd Numbers = 25

Use [text] element or @: to switch between c# code and literal text
@for (int i = 1; i [= 10; i++)
{
[b]@i[/b]
if (i % 2 == 0)
{
[text] – Even [/text]
}
else
{
[text] – Odd [/text]
}
[br /]
}

The above program can be re-written using @: as shown below.
@for (int i = 1; i [= 10; i++)
{
[b]@i[/b]
if (i % 2 == 0)
{
@: – Even
}
else
{
@: – Odd
}
[br /]
}

Output:
1 – Odd
2 – Even
3 – Odd
4 – Even
5 – Odd
6 – Even
7 – Odd
8 – Even
9 – Odd
10 – Even

Comments

mhd says:

can i include packges in Razer View

M Khan says:

Dear Kudvenkat,
Can you please any videos on PetaPoco and Telerik grid views?

Zeinab Abd Elmonam hassan says:

can you tell me how to post form has more than one value to controller
i tried to do this but it doesn’t take the user entered data
any help please ?

mhd says:

whene you will start with MVVM C# tutorials
ANTWORTEN

mhd says:

great lesson

Petr Semkin says:

yees, looks like I am becoming a fan of your playlist

Sunil datla says:

nice and easy explanation

krismaly says:

I love your explanation of syntax with examples and it is very clear. I love this video and also I recommend all developer community to watch. Thanks a lot

Ahmet Yaman says:

Thank you sir.

kudvenkat says:

Thank you very much for taking time to give feedback. For email alerts, when new videos are uploaded, please subscribe to my channel. In the description of this video, I have included the link for ASP .NET, C#, and SQL Server playlists. All the videos are arranged in logical sequence in these playlists, which could be useful to you. Please share the link with your friends who you think would also benefit from them. If you like these videos, please click on the THUMBS UP button below the video.

 Write a comment

*

Do you like our videos?
Do you want to see more like that?

Please click below to support us on Facebook!