Mar 13, 2011

Quick View of ASP.NET MVC 3 Razor View Engine

ASP.NET MVC 3 introdues a new view-engine option called “Razor” which enables you to quickly integrate server code into your HTML markup with a minimum of keystrokes. Unlike most template syntaxes, you do not need to interrupt your coding to explicitly denote server blocks within your HTML. This enables a really compact and expressive syntax which is clean, fast and fun to type. Here are the quick view related to Razor view-engine in asp.net MVC 3.0.

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 pageasp.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 <%-- --%>) )
@RenderBodyused in layout where we want the views based on this layout to “fill in” specific body content for a requested URL.
@RenderSectiondefine 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
@tempdataTo 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.

Enjoy ASP.NET MVC !!!