Quick View of ASP.NET MVC 3 Razor View Engine

时间:2022-09-12 15:12:53

转载:http://techbrij.com/565/quick-view-of-asp-net-mvc-3-razor-view-engine

 

File Extension
.cshtml for c# and vbhtml for vb.net

_(underscore)
Files that cannot be shown by direct requests (master pages, partial views etc) have underscore (_) prefix in their names. e.g. _Layout.cshtml

@
start of a server side code block. It doesn’t require you to explicitly close the code-block. (short-hand of <%= %>)

@{ code }
(Multi-line Statements) add multiple lines of server side code by wrapping it within a @{ code } block.

@( )
(Multi-Token Statements) enables a code block to have multiple tokens

@:
to explicitly indicate the start of content in the line

<text>
to render multi-line content blocks

layout page
asp.net master page is recognized in MVC

Layout <file>
layout we want to use within a ASP.NET MVC

@* comment *@
To define server side comment (like <%– –%>) )

@RenderBody
used in layout where we want the views based on this layout to “fill in” specific body content for a requested URL.

@RenderSection
define two sections within our layout that the view templates within our site can optionally choose to fill-in

@section { }
To define section

@model<StrongModelType>
define strongly-typed models

@ViewBag.<myData>
New Dynamic Controller property, It internally stores the property name/value pairs within the ViewData dictionary. It is used to pass Data Between a Controller and a View

@tempdata
To pass State Between Action Methods

Standard HTML Helpers(not a complete list)

* Html.ActionLink()
* Html.BeginForm()
* Html.CheckBox()
* Html.DropDownList()
* Html.EndForm()
* Html.Hidden()
* Html.ListBox()
* Html.Password()
* Html.RadioButton()
* Html.TextArea()
* Html.TextBox()

Other Helpers
Chart,
Crypto,
WebGrid,
WebImage,
WebMail, etc.