MVC Razor View Engine

Source: Internet
Author: User

Razor is not a programming language. It is a server-side markup language.

Razor is a markup syntax that allows you to embed server-based code (Visual Basic and C #) into a Web page

When a Web page is written to the browser, the server-based code can create dynamic content. When the Web page loads, the server executes the server-based code within the page before returning the page to the browser. Because it is running on a server, this code can perform complex tasks, such as accessing a database.

Razor has a significant advantage in reducing code redundancy, enhancing code readability, and vs IntelliSense. Razor is loved by all of the ASP's developers once it's launched.

The syntax used by Razor is similar to PHP and ASP.

Grammar rules

Razor code encapsulated in @{...}

In-line expressions (variables and functions) begin with @

The code statement ends with a semicolon

string surrounded by quotation marks

C # code is case sensitive

C # files have the. cshtml extension.

When adding a view, select the Razor view engine to

Usage:

var " Word!!!! " ;}          @for (int03; i++)          {                          }        @if (true)        {                      }

How to work:

When the server reads this page, the Razor code is run first before the HTML page is sent to the browser. The code that executes on the server can accomplish tasks that cannot be done in the browser, such as accessing the server database. Server code can create dynamic HTML content before the page is sent to the browser. From the browser, the HTML generated by the server code is no different from the static HTML content.

Get user input:

Razor Data Load Changer

Cycle:

Create an action named loop to define a collection and then iterate through the razor

Loop.cshtml Code:

Note: In the code, where tags and code are allowed to be in a form, Razor has a very good performance in IntelliSense.

Dynamic type: You can store any type of value in a Dynamic Data type variable. The type checking of these variables occurs at run time.

ViewData is a dictionary type, which is assigned in dictionary mode, and the value is read by key, viewdata["MyName"]

2, ViewBag is a dynamic type, the use of directly add property assignment can be viewbag.myname

3, ViewBag, and ViewData are only valid in the current action, similar to ViewState (objects on a page in WebForm ... )

4. Values in ViewData and viewbag can be accessed by each other because the ViewBag implementation contains ViewData

Action:

View:

MVC Razor View Engine

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.