MVC 4 Razor syntax explained and used

Source: Internet
Author: User

Classification of Razor grammar

@using: Introducing namespaces

@Model: Declaring a strongly typed data model type

@section: Define the information for the section to implement the master page

@RenderBody (): When you create a page-based layout, the contents of the view are merged with the layout page, and the contents of the newly created view are rendered between the labels through the @renderbody method of the layout page.

@RenderPage: Renders a page. For example, a fixed head in a Web page can be placed in a shared view file, and then in the layout is also exempt from this method call, such as @RenderPage ("~/views/shared/_header.cshtml")

@RenderSection: The layout page also has the Concept of node (section) to facilitate partial rendering

1. In-line (inline) C # (server-side code) Form (inline code)

@ variables such as: @User. Name, after the end of the variable or property, any form of text or tags are automatically parsed into HTML, if the end of the judgment is ambiguous, please add parentheses as follows

@ (expression) For example: @ (i + 1) or @ (user.name) parentheses: expression, or variable

The @ method calls for example: @Html. TextBox ("UserName"). ToString ()

2. Code block

First case:

@{code block}

@if (condition) {code block}

@Switch (condition) {Branch match code block}

@for (loop control) {code block}

@foreach (loop control) {code block}

@while (loop control) {code block}

@do {code block} while (loop control) The code inside the code block must follow the C # syntax strictly, and the end of each line must have a semicolon "

Second case: (text in code block or HTML client tag)

Single-line text

@: Inline text wrapping automatically becomes C # server-side code.

Multiple lines of text

Any label pairs such as:<div> multiline text </div>

Any self-closing tags such as:

Plain text uses pseudo -tags <text> For example:<text> multiline text, and the labels on both sides are not output </text>

3. Other

e-Mail can be automatically identified, for example: [email protected]

Be mistaken for an email address in parentheses (), for example: 123456 (@UserName)

To export trademark copyrights and so on, please use the @ character escape, for example: @@→ will output a single character @

MVC 4 Razor syntax explained and used

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.