ASP. NET mvc3 razor view engine-basic syntax

Source: Internet
Author: User
Tags define local visual studio 2010
Document directory
  • II: razor view engine-basic syntax

I: Changes in ASP. NET mvc3 in Visual Studio 2010

The creation of an mvc3 project in vs2010 shows significant changes with MVC2.

  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";
}

<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 ";", for example:

@{
var i = 10;
var y = 20;
}

@ Xxx does not need to end with ";", for example:

@ 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 Encoding & #64;
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: The output of variable concatenation.

@ {Var I = 10 ;}
<P> text @ I text </P>
Output text 10 Text

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 -->

End now! Call.

Related Article

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.