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

<span>@model.Message</span>

Explicit Code Expression


<span>ISBN@(isbn)</span>

Unencoded Code Expression

<span>@Html.Raw(model.Message)</span>

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>

 }

or

@if (showMessage) {

 @:This is plain text.

 }

Escaping the Code Delimiter

My Twitter Handle is &#64;hacked

or

My Twitter Handle is @@haacked

Server Side Comment

@*

 This is a multiline server side comment.

 @if (showMessage) {

 <h1>@ViewBag.Message</h1>

 }

 All of this is commented out.

 *@

Calling a Generic Method

@(Html.SomeMethod<AType>())

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)’;
$(“#message”).html(message).show(‘slow’);
});
</script>