Basic syntax for 1.Razor
@* Multiple lines of code need to be included in curly braces {} and after each sentence code will need to add a semicolon; *@@{viewbag.title = "Index"; Viewbag.name = "Linq"; Viewbag.isok = false; List<string>List = new List<string>() {"11", "22", "33", "44", "55", "66"}; Viewbag.description = "<spanstyle= ' Font-weight:bold; '>The Lost Tooth</span>";} @* a single line of code does not require a semicolon end *@<P>@DateTime. Now</P><P>Name: @ ("Welcome," + viewbag.name) Status: @ (Viewbag.isok? "Yes": "no") @ @Linq</P>@if (Viewbag.isok = null && Viewbag.isok) {@: Enable}else{@: Deactivate} @if (Viewbag.isok! = null && ViewBag . IsOk) {<span>1. Enable</span>}else{<span>2. Deactivate</span>} @if (Viewbag.isok! = null && viewbag.isok) {<text>1234567<span>1. Enable</span> </text>}else{<text>1234567<span>2. Deactivate</span> </text>}<ul>@foreach (var item in list) {<Li>@item</Li> }</ul>@* All Razor syntax output variables will be HTML encoded (HTMLEncode) after the output, if you want the HTML tag inside to take effect to call Html.raw method *@ @ViewBag. Description@Html.Raw ( viewbag.description) @{<H2>Mixed example @ (viewbag.name)</H2>foreach (var item in list) {<Li>@item</Li> } }
2.Razor Motherboard page Frame
2.1 Razor Page Execution order
After controller is passed back to Viewresult to Mvchandler, Mvchandler will try to find the corresponding view page, then razor the page execution life cycle
Mvchandler found Razor page will be executed first, then check whether the view page has layout properties
2.2 About _viewstart
The 2.2.1/view/_viewstart.cshtml file is truncated before any view is truncated in the/view/directory, and any view subdirectory with the same name as the controller can appear with the same _ Viewstart.cshtml so that different controllers can correspond to different _viewstart.cshtml
2.3 _layout Layout Page
2.3.1 _layout.cshtml and general razor of the View page, the difference is that there are 2 razor syntax, respectively, is @renderbody (pre-hole) and @rendersection (named pit)
[Email protected] helper method
Razor provides a convenient syntax that allows you to extract part of the content or part of the code from the view page into an independent helper method
< P > </ P > @helper ShowTime () { @DateTime. Now;}
[Email protected] custom functions
In @helper You cannot customize the properties, you can simply pass in the parameters, and then format the image you want to render directly out. So Razor also provides @functions custom function functions
< P > </ P > @functions {public ihtmlstring ShowTime () { return new Htmlstring (DateTime.Now.ToString ());}}
[email protected] referencing reference categories
In the Razor page, you can refer to the namespace at the top of the page via @using, which will be used in the view page.
@using Mvcapplication1.models
<pagesvalidaterequest= "false"Pageparserfiltertype= "System.Web.Mvc.ViewTypeParserFilter, SYSTEM.WEB.MVC, version=4.0.0.0, Culture=neutral, publickeytoken= 31bf3856ad364e35 "PageBaseType= "System.Web.Mvc.ViewPage, SYSTEM.WEB.MVC, version=4.0.0.0, culture=neutral, publickeytoken=31bf3856ad364e35" Usercontrolbasetype= "System.Web.Mvc.ViewUserControl, SYSTEM.WEB.MVC, version=4.0.0.0, Culture=neutral, publickeytoken= 31bf3856ad364e35 "> <Controls> <AddAssembly= "SYSTEM.WEB.MVC, version=4.0.0.0, Culture=neutral, publickeytoken=31bf3856ad364e35"namespace= "SYSTEM.WEB.MVC"TagPrefix= "MVC" /> </Controls> <Namespaces> <!--can also be configured in the configuration file - <Addnamespace= "Mvcapplication1.models"/> </Namespaces> </pages>
[email protected] referencing reference types
PublicActionResult Index () {List<User> list =NewList<user>(); List. ADD (NewUser () {name=" One", age= One}); List. ADD (NewUser () {Name =" A", age = A }); List. ADD (NewUser () {Name =" -", age = - }); returnView (list); }
@using mvcapplication1.models@{ var data = (IEnumerable<User>) Model;} < ul > @foreach (var item in data) { <li>@item. Name</li> }</ul>
@using Mvcapplication1.models@model IEnumerable < User > < ul > @foreach (var item in Model) { <li>@item. Name</li> }</ul>
7. Layout page
<!DOCTYPE HTML><HTML><Head> <Metaname= "Viewport"content= "Width=device-width" /> <title>@ViewBag. Title</title></Head><Body> <DivID= "header">@if (issectiondefined ("header")) {@RenderSection ("header", false)} else { <H1>No padding @rendersection ("header", false)</H1> } </Div> <Div>@Html. Partial ("_layoutuser")</Div> <Div>@RenderBody ()</Div> <DivID= "Footer">@RenderSection ("footer", false)</Div></Body></HTML>
@{ viewbag.title = "about"; Layout = "~/views/shared/_layouttest.cshtml";} < H1 > I am @renderbody ()</H1>@section footer { < H1> I am @rendersection ("footer", false)</H1> }
Razor syntax in ASP.