ASP. NET MVC Learning Note-2. Razor syntax

Source: Internet
Author: User

Tag: The Mode property is the first foreach render. NET situation you

1. Expressions

The expression must follow the "@" symbol,

2. code block

The code block must be in "@{}", and each line of code must end with ";". Variables defined in a code block may be used by other blocks in the same domain. For example, variables defined at the top of the view can be accessed by code blocks and code snippets in the same view.

3. Layout

Razor maintains the consistency of Web page appearance layout through layouts. A layout template contains basic labels, and you can specify where to render the view content. Like what

Basic layout file (_layout.cshtml)

<!DOCTYPE Html><HTMLLang= "en">    <Head>        <meteCharSet= "Utf-8"/>        <title>@View. Title</title>     </Head>    <Body>        <Divclass= "header">@RenderSection ("Header"); </Div>@RenderBody ()<Divclass= "Footer">@RenderSection ("Footer"); </Div>    </Body></HTML>

When the layout page definition is complete, other view pages can reference the layout file, such as

@{layout= "~/_layout.cshtml";} @section Header {    <H1>Page header Content</H1  >} @section Footer {    Copyright @DateTime. now.year}<class = "Main" >     Page Main Content</div>

Use the razor layout and content view to group pages together, showing a complete page with each block defining different parts of the page.

4. Partial views

Using layouts to make Web site appearance consistent by reusing part of the HTML code, but in some cases the layout cannot be implemented, for example, a portion of the information on a Web page needs to be repeated (in a consistent, inconsistent display), for example, a list of transactions on a shopping site page, showing only the trade name, Current price and summary information.

ASP. NET MVC implements this requirement through a partial view of the technology.

First, you define a partial view and save it as a separate view file (for example, ~/views/shared/acution.cshtml).

@model Auction<Divclass= "Auction">    <ahref= "@Model. Url"><imgsrc= "@Model. ImageUrl" </a>    <h4><ahref= "@Model. Url">@Model. Title</a></h4>    <P>Current Price: @Model. currentprice</P></Div>

Then, in the location where you want to use that part of the view, call it using an HTML method that comes with ASP, such as:

@model IEnumerable<Auction><H2>Search Result </ H2 > @foreach (Var auction in Model) {    @Html. Partial ("auction", auction);}

Where the first parameter of the Html.partial () method, "Auction", is a partial view name and needs to contain the extension. The second parameter is a data model that is passed to a partial view. The second parameter is not required, and if not passed, the data model that calls that part of the view is passed by default, for example, ienumerable<auction> in this case.

This shows the use of partial views to reduce code duplication and coding complexity and enhance readability in Web pages.

5. Display Data

The MVC architecture is divided into three layers, models, views, and controllers. The three layers are separated from each other and work together, which requires the Controller to act as a "coordinating" role, the view takes the request to the controller, the controller operates on the model, and the results of the operation are fed back to the view, and the view renders the data of the model.

The way data is passed between the controller and the view, ASP. NET MVC provides the following implementation methods:

1) ViewData

ViewData implementation, which is similar to the dictionary operation, makes data transfer very simple.

Use the viewdata["DataKey"]=datavalue in the Controller method to assign values, and in the view file, use var datavalue=viewdata["DataKey" to get the data.

2) ViewBag

The use type of viewbag and the dynamic type in C #, you can manipulate its properties directly, for example,

Controller method: Viewbag.dataproperty=datavalue;

View file: Var Datavalue=viewbag.dataproperty;

3) Model Property

The Model property is strongly typed and is a dynamic type and can be accessed directly by entering "@Model" on the view.

6. htmlhelper and Urlhelper

The goal of a Web request is to send HTML code to the user, and in the Razor syntax, ASP. NET MVC has two important helper classes to generate the corresponding HTML code, respectively, HtmlHelper and Urlhelper. The HtmlHelper class is used to generate HTML markup code, and Urlhelper is used to generate URL address links.

<src= ' @Url. Content ("~/content/images/header.jps") '/>@ Html.ActionLink ("Home", "Index", "Home")

The resulting HMTL code is:

<src= '/vdir/content/images/header.jpg '/><  href = "/vdir/home/index" > Homepage</a>

ASP. NET MVC Learning Note-2. Razor syntax

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.