ASP. NET MVC3 Razor syntax

Source: Internet
Author: User

Changes in asp. NET MVC3 in Visual Studio 2010

The creation of an MVC3 project in VS2010 shows significant changes with the previous MVC2 project.

1. The necessary runtime environment for ASP. NET MVC3 is. NET 4.0 (to use MVC3 in 3.5, there is no way !) 2. The default MVC3 template project is integrated

3. Brand new Razor view Engine

@{

ViewBag. Title = "Home Page ";

}

<H2> @ ViewBag. Message

<P>

To learn more about ASP. net mvc visit

<A href = "http://asp.net/mvc" title = "ASP. net mvc Website">

Http://asp.net/mvc

</A>.

@ ServerInfo. GetHtml ()

</P>

4. All documents starting "_"

ASP. NET 4 by default, all access addresses starting with "_" are denied _*. cshtml documentation. about *. the cshtml document is actually part of WebMatrix. We will introduce the instructions for using this document in detail later.

For example, access http: // localhost: 7985/_ ViewPage1.cshtml

II: Razor view engine-basic syntax

-Basics-

All codes starting with @ or @ {/* Code body */} (no spaces can be added directly to @ and {) will be processed by the ASP. NET engine.
Each line of code in @ {/* Code body */} must end with ";", as shown in
@{
Var I = 10;
Var y = 20;
}
@ Xxx does not need to end with ";", as shown in figure

@ I output 10

@ Y; output 20;
Uppercase/lowercase letters in the code area.
Character Type constants must be enclosed by "", for example, @ {string str = "my string ";}

-Note-

To output the "@" character on the page
You can use html ascii code @
Of course, Razor also provides the smart analysis function: if the first character of @ is not a blank character, ASP. NET will not process it.

For example, <p> text @ I xx </p> outputs text @ I xx.

Single Row Syntax:

@ {Var I = 10 ;}
Multiline Syntax:

@{

Var I = 10;

Var y = 20;

}

1. When using local variables, Razor does not support access modifiers (public, private, etc.), which makes no sense)
Define local variables on a single row
@ {Var total = 7 ;}
@ {Var myMessage = "Hello World ";}
Define local variables on multiple rows

@{

Var greeting = "Welcome to our site! ";

Var weekDay = DateTime. Now. DayOfWeek;

Var greetingMessage = greeting + "Today is:" + weekDay;

}

Use variables in the context

<P> The value of your account is: @ total </p>

<P> The value of myMessage is: @ myMessage </p>


Note:
:Variable concatenation output

@ {Var I = 10 ;}

<P> text @ I text </p>
But what if you want to output text10text?

<P> text @ {@ I} text </p>

<P> text @ I text </p>

<P> text @ itext </p>

<P> text @ itext </p>

If the method name of the output variable does not need to be included in @ {}, it can take effect as follows:
<P> text@ I .ToString () text </p>
You can directly write the variable object: @ var1 @ var2 @ myObject. xx

2. Use logical processing

@{

If (xx)

{

// Do something

}

Else

{

// Do anything

}

}

3. Use html tags inside @ {...}

@{

<P> text </P>

<Div> div1 </div>

}

4. output text inside @ {...}
Use @: to output a single line:

@{

@: This is some text

@: This is text too

@: @ I can also output variables

}

Use <text/> to output multiple rows:

@{

<Text>

Tomorrow is good

Some girl is nice

</Text>

}

5. Use comments inside @ {...}

@{

// Single line comment

Var I = 10;

// Defg

}

@ * Multi-line comment *@

@*

Multi-line comment

Multi-line comment

*@

@{

@*

Multi-line comment

Multi-line comment

*@

Var I = 10; @ * asdfasf *@

}

<! -- You can also use C # default/*... */-->

@{

/*

Multi-line comment

*/

}

If it is used inside @ {...} <! --> Comment, it is output to the page. If it is in <! --> If @ variable is used internally, it will be processed.
@{

<! -- Time now: @ DateTime. Now. ToString () -->
}
Output: <! -- Time now: 4/9/2011 12:01 -->

6. type conversion
AsInt (), IsInt ()
AsBool (), IsBool ()
AsFloat (), IsFloat ()
AsDecimal (), IsDecimal ()
AsDateTime (), IsDateTime ()
ToString ()
Example:

@{

Var I = "10 ";

}

<P> I = @ I. AsInt () </p> <! -- Output I = 10 -->

7. Use loop

<! -- Method 1 -->

@ For (int I = 10; I <11; I ++)

{

@: @ I

}

<! -- Method 2 -->

@{

For (int I = 10; I <11; I ++)

{

// Do something

}

}

<! -- While similarly -->

Refer to: http://www.ityoudao.com/Web/AspNet_582_1598.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.