MVC3 - Using layout for views

You can specified view to use layout as following:


 Layout = “~/Views/Shared/_Layout.cshtml”;



Or you can override the _ViewStart.cshtml file within the view folder using the above.

MVC3 -Razor syntax sample

Implicit Code Expression


Explicit Code Expression


Unencoded Code Expression


Code Block


int x = 123;

string y = “because.”;


Combining Text and Markup

@foreach (var item in items) {

 <span>Item @item.Name.</span>


Mixing Code and Plain Text

@if (showMessage) {

 <text>This is plain text</text>



@if (showMessage) {

 @:This is plain text.


Escaping the Code Delimiter

My Twitter Handle is &#64;hacked


My Twitter Handle is @@haacked

Server Side Comment


 This is a multiline server side comment.

 @if (showMessage) {



 All of this is commented out.


Calling a Generic Method


Setting variables in JavaScript using JavaScript string encode

Use the @Ajax.JavaScriptStringEncode to encode the input.

<script type=”text/javascript”>
$(function () {
var message = ‘Hello @Ajax.JavaScriptStringEncode(ViewBag.Username)’;