An example Analysis of Razor Grammar of ASP.NET MVC
This article mainly introduces the example analysis of ASP.NET MVC's Razor grammar, which is very detailed and has certain reference value. Friends who are interested must finish it!
1. Expression.
The expression must follow the "@" symbol
two。 Code block
The code block must be in "@ {}" and each line of code must end with ";". Variables defined in the code block may be used by other blocks in the same domain. For example, variables defined at the top of a view can be accessed by code blocks and snippets in the same view.
3. Overall Arrangement
Razor maintains consistency in the appearance and layout of web pages through layouts. The layout template contains basic labels and can specify the location of the rendered view contents. such as
Basic layout file (_ Layout.cshtml)
@ View.Title @ RenderSection ("Header"); @ RenderBody () @ RenderSection ("Footer")
After the layout page is defined, other view pages can reference the layout file, such as
@ {Layout= "~ / _ Layout.cshtml";} @ section Header {Page Header Content} @ section Footer {Copyright @ DateTime.Now.Year} Page Main Content
The pages are grouped together using the Razor layout and content view to show a complete page, each of which defines a different part of the page.
4. Partial view
Use the layout to achieve consistency in the appearance of the site by reusing part of the HTML code, but in some cases, the layout cannot be achieved, for example, a part of the information on the page needs to be repeated many times (consistent format, inconsistent display content), for example, the list of transactions on the page of the shopping site shows only the transaction name, current price and summary information.
ASP.NET MVC implements this requirement through the technology of partial views.
First, define a partial view and save it as a separate view file (for example, ~ / Views/Shared/Acution.cshtml).
@ model Auction