MVC4 related Razor syntax and form form (reproduced)

Source: Internet
Author: User

Razor Layouts (layout)

The default project is built with a _viewstart.cshtml file, the code in the file is as follows:

[CSharp]View Plaincopy
    1. @{Layout = "~/views/shared/_layout.cshtml";}

Here's the @{layout= "file path";} the code block specifies the layout file () that is used by default for the entire project

The blue section on the left shows the default layout file provided by MVC4, and we look at the contents of _layout.cshtml:

It is not difficult to see, _layout.cshtml inside is the HTML static page. For some of the syntax inside do a simple explanation:

@RenderBody () This layout will be used for all pages by default (webform template)

@RenderBody () All content that is equivalent to one placeholder for other pages will be rendered by the engine in this place.

@RenderSecion () This placeholder means that a section inside the page is rendered (either HTML code or a combination of C # code and HTML).

Note: What if you don't want to use the default layout, or if your Web app has multiple page layout files?

In fact, it is very simple, for example: You create a new view page userinfo.cshtml, you can write in the userinfo.cshtml file

@{layout = null;}

That's it, and this time it won't apply the layout file, if you want to define a new layout (in accordance with the customary principle), we create our own layout under views/shared. mylayout.cshtml file, and then we assign the following layout to the new page we created:

@{Layout = "~/views/shared/_mylayout.cshtml";}

For the above

@RenderSection ("Scripts", Required:false) to make a supplementary note, the first parameter indicates: The name of the section being rendered on the child page, the second parameter: Specifies whether the sub-page section is required, if required:true is specified; However, in the sub-page does not give the name of the section, compile is not a pass.

If we want to use the default section in the master page, we can make the following judgments:

[CSharp]View Plaincopy
    1. @if (issectiondefined ("sectionname"))
    2. {
    3. @RenderSection ("sectionname")
    4. }
    5. Else
    6. {
    7. <span> default Section </span>
    8. }

If the sub-page has this section we will render the sub-page, no words will show our default HTML block (for example: The site's head, bottom, sidebar, etc.).

Classification of Razor grammar

Basic syntax

[CSharp]View Plaincopy
    1. @using: Introducing Namespaces
    2. @model: Declaring a strongly typed data model type
    3. @section: Define section information to implement a master page
    4. @RenderBody (): When you create a view based on this layout page, 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.
    5. @RenderPage: Renders a page. For example, the fixed head of the Web page can be placed in a shared view file, and then in the layout page through this method call, use the following:
    6. @RenderPage ("~/views/shared/_header.cshtml")
    7. @RenderSection: The layout page also has the concept of sections (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) inside the parentheses are: expressions, or VariablesThe @ 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 @

HtmlHelper and Ajaxhelper

Two ways to Html.BeginForm:

The first uses a using statement:

[CSharp]View Plaincopy
    1. @using (html.beginform ())
    2. {
    3. }

The second is a closed form of a block of code (note that form is not in-line code as a block of code: So there must be a semicolon at the end)

[CSharp]View Plaincopy
    1. @{html.beginform ();}
    2. @{html.endform ();}

Ajax.beginform () has only one of the following: Because I don't see ajax.endform (). Oh, it is estimated that Microsoft's development team developed the Razor engine when the missing out

[CSharp]View Plaincopy
    1. @using (ajax.beginform (new Ajaxoptions {updatetargetid="", Onsuccess=""})
    2. {
    3. }

In order to make the Ajax form valid, we have to introduce the AJAX-enabled JS script on the View page. As follows:

[CSharp]View Plaincopy
    1. @Scripts. Render ("~/bundles/jqueryval")

This is the Microsoft Script binding technology, actually bundled is (the following JS code)


It's not hard to see a JavaScript script with Ajax in it.

For Ajaxform, specify the commit parameter:

When the asynchronous request succeeds, the HTML tag content of the client specified by the Updatetargetid is updated locally using the content returned by the backend, and then the JS method of the specified client is called onsuccess.

Specific parameter descriptions can be referred to MSDN:

Onsuccess:http://msdn.microsoft.com/zh-cn/library/system.web.mvc.ajax.ajaxoptions.onsuccess (v=vs.108). aspx

A little supplement to razor IntelliSense

On Microsoft's cshtml page, IntelliSense is automatically present when you follow some objects

When we customize a class, let's think of this class when the razor engine has only the ability to perceive. We can find

Views/web.config file, open the file we will find the following node:

[HTML]View Plaincopy
  1. <System.web.webPages.razor>
  2. <host factorytype="System.Web.Mvc.MvcWebRazorHostFactory, SYSTEM.WEB.MVC, version=4.0.0.0, Culture=neutral, publickeytoken=31bf3856ad364e35 " />
  3. <pages pagebasetype="System.Web.Mvc.WebViewPage">
  4. <namespaces>
  5. <add namespace="SYSTEM.WEB.MVC" />
  6. <add namespace="System.Web.Mvc.Ajax" />
  7. <add namespace="System.Web.Mvc.Html" />
  8. <add namespace="System.Web.Optimization"/>
  9. <add namespace="System.Web.Routing" />
  10. </Namespaces>
  11. </pages>
  12. </System.web.webPages.razor>

We just have to add the namespace where our class is located.

Some practical features of the razor (great flexibility)

Partial page output Caching (partially-paged out cache)

We can use @html.action () to request a processing method and render the returned model or ViewModel object to the page.

By using the OutputCache feature, we implemented a cache of objects. Therefore, when duplicate requests occur, it is possible to automatically output cache information to avoid querying the database frequently, reduce the burden on the server and improve the response speed.

c#3.0 new Features

MVC4 related Razor syntax and form form (reproduced)

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.