Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

This tutorial will help you to get started with ASP.NET MVC

Getting started with ASP.NET MVC

Getting Started with ASP.NET MVC - Part 5: How to do programming with razor syntax

May 15 2012 12:00AM by Jalpesh   

In previous chapter, we have learnt about different view engines. In this chapter, we are going learn about how to do programming with the Razor syntax.

What is razor syntax?

As we all know, with ASP.NET MVC3 Microsoft has introduced new view engine, Razor, which was developed to simplify earlier aspx view engine syntax. Here you can write things @character, which called magic character in razor syntax. In razor view engine, you have .cshtml and .vbhtml for view. Where .cshtml is for C# language and .vbhtml is for VB.NET language.

Fundamentals of razor syntax:

  • @character is most important character in the Razor Syntax. You can do many things with @Character. Like to print the current datetime you can use @character as shown below:
    Current DataTime: @DateTime.Now
    
    It will print current date time on the browser.

Date Time

  • You can also write single line statement and variable declaring with @{} syntax as shown below:

    @{ var number = 10; }
    
    
    Number is: @number 
    

    It will print number in browser as below:

Numbers

  • Same way you can also have multiline syntax with @{} syntax like following.
    @{ 
    var firstName = "jalpesh";
    var lastName="vadgama";
    var nameMessage = string.Format("My full name is : {0} {1}", firstName, lastName);
    }

    My name is :@nameMessage 

It will print the name as shown below:

Full Name

  • You can also print a path with “\” character. It supports escape character also.
    @{ var filePath = @"C:\ApplicationFolder\"; }

    The file path is: @filePath

It will print filepath in browser as follows:

Escape Sequence

Therefore, from all above syntax you can see @ is a magic character for the razor syntax and you can print anything with @character.

Advance syntaxes for razor:

  • HTML Expressions in razor syntax:

    Like other view engine, @razor syntax also supports the HTML expression seamless. You can write composite code with html tags and css etc. @ character with razor syntax supports it all.

    Some of the examples are:

    @{ var name = "Jalpesh"; }

    The Name: @name

It will print name in italic characters.

  • IF and Else loop with razor syntax:

    Like any other programming language Razor syntax also supports the ‘if and else’ loop. We can write if and loop like following.

    @{
    var message = string.Empty ;
    if(IsPost)
    {
    message = "Page was postback";
    }
    else
    {
    message = "Page was not posback";
    }
    }
    
    
    
    Posback Status:@message 
    

    As you can see in the above code I have checked IsPost with ‘if and else’ loop and assigning a message-to-message variable. In addition, I have a button for postback. Also, I have taken a form with post method on a submit button once we click it will post back the form. So first, we run application without postbacking the form. It will print message in browser like following:

No Postback

Now once you click on submit button it will look like following:

With Postback

  • For loop in Razor Syntax:

    Razor syntax supports for loops also. Let’s take a simple example of for loop and then we will print a number with help of for loop.

    @for(var i = 1; i < 10; i++)
    {
    

Number @i

}

This will print number like following in browser:

For Loop

  • For each loop:

    For each loop in razor syntax look like following. Here I am going print all the variable collection.

    @foreach (var variables in Request.ServerVariables)
    {
    }

It will print server variables name like following.

For each loop

  • Switch case statement in Razor Syntax:

    Following is a example of switch case syntax and based on case selected it will print the output.

    @{
    var number=1; var message = string.Empty;
    switch(number)
    {
    case 1:
    message = "Number is 1";
    break;
    case 2:
    message = "Number is 2";
    break;
    default:
    message = "Number is 0";
    break;
    }
    

@message

}

It will print case 1 message as we have number assigned to 1 in browser like following:

Switch Case

  • Commenting in Razor Syntax:

    Like any other languages, Razor syntax also have comments. Following are the syntaxs for single line and multi-line comments.

    @* single line comment. *@

    @*
    Multi - Line
    Comment
    *@

That is it. As you can see Razor syntax is one of easiest syntax to learn. Hope you liked it. Stay tuned for next chapter. In next chapter, we are going to learn about entity framework and how we can use that in ASP.NET MVC.


Jalpesh
15 · 11% · 3478
6



Submit

Your Comment


Sign Up or Login to post a comment.

"Getting Started with ASP.NET MVC - Part 5: How to do programming with razor syntax" rated 5 out of 5 by 6 readers
Getting Started with ASP.NET MVC - Part 5: How to do programming with razor syntax , 5.0 out of 5 based on 6 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]