Working with Razor syntax

First Install asp.net mvc3 software

Creating a new MVC application using the Razor view engine. Go to vstd.net

    ↓

Click on file

    ↓

Click on new

    ↓

Click on project

    ↓

Select asp.net.mvc 3 web application

    ↓

Type application name

    ↓

Click on ok Select the project template as internet application Select the view engine as Razor Click on ok   2When we create Razor application there do not we any deference 2Only deference is with in the view all view pages are created . CSHTML extension . where very view page will contain plain HTML code. 2So that Rendering will be much more minimized reduced.   --.Razor syntax will take 3 techniques to write

  1. Single line blocks
  2. Multi line blocks
  3. Inline Expressions

  2in general syntax of Razor is for single line blocks

Syntax:

@  {statement 1 ;} @  {statement 2 ;}    

EX :-  1. @ {var  a = 10;} @ {var  b = 20.5} @  {var c = ‘satya’}

  1. @ {var a = 10;}

@ {double b = 20.5} @   {String c = ‘satya’}   2We use the following syntax for multiple blocks

Syntax:

@ {  statement 1 ;} statement 2; }

Ex :-

  1. @ { var  a = 10;

Var   b = 20.5; Var c = ‘satya’ }

  1. @ {var a = 10;}

@ {double b = 20.5} @   {String c = ‘satya’}  

Example  with Razor syntax:-

  2Go to solution explorer

       ↓

Select Home folder

       ↓

Click with RM button

       ↓

Click on Add

       ↓

Click on view

       ↓

Type the view name as (Sample)

       ↓

De active check box use  view  (or) Layout

       ↓

Click on add

       ↓

A new view page is created with the name sample. HTML

       ↓

Go to source

       ↓

Write the following code.  On the top of page

@ { layout = null; } @ { Var a =10; Var b = “satya”; Var c = “20.5” }

<body>

<h2> satya technologies </h2>

<div>

<p> value of a a is :- @ a </p>

<p> value of a b is :- @ b </p>

<p> value of a c is :- @ c </p>

</div> </body>

 

Learn ASP.net by Tekslate - Fastest growing sector in the industry. Explore Online "ASP.net Training" and course is aligned with industry needs & developed by industry veterans. Tekslate will turn you into ASP.net Expert.

Go to the Home controller. cs  create an action method name as the sample

Public action result sample ()

{

Return view ();

}

Go to global .aspx

Change the action name as sample and check and run the application

--> Create a new view page (Sample)

Write the following code for Razor syntax and design

@ { layout = null;

Int num 1, num2 , result = 0;

If (is post)

{

Num 1 = convert to int 32 (Request [“T1”]);

Num 2 = convert to int 32 (Request [“T2”]);

Result = num 1 + num2;

<form .action = “”Method = “post”>

Enter first number

<input type = ‘text’@ “T1”/> <Br>

Enter Second number

<input type = ‘text’@ “T2”/> <Br>

Result is : @ Result

<input type = ‘submit’ id = “B1” value = “Add”/>

</form>

For in-depth understanding click on