Sunday, January 9, 2011

Razor Syntax Sheet

Syntax/Sample Web Forms Razor
Code Block
<%
  int x = 123; 
  string y = "because."; 
%>
@{ 
  int x = 123; 
  string y = "because.";
}
Expression (Html Encoded)
<%: model.Message %>
@model.Message
Expression (Unencoded)
<%= model.Message %>
@Html.Raw(model.Message)
Combining Text and markup
<% foreach(var item in items) { %>
  <%: item.Prop %>;
<% } %>;
@foreach(var item in items) {
  @item.Prop 
}
Mixing code and Plain text
<% if (foo) { %> 
  Plain Text 
<% } %>
@if (foo) {
  Plain Text 
}
Mixing code and plain text (alternate) Same as above
@if (foo) {
  @:Plain Text is @bar
}
Email Addresses Razor recognizes basic email format and is smart enough not to treat the @ as a code delimiter
Hi philha@example.com
Explicit Expression In this case, we need to be explicit about the expression by using parentheses.
ISBN@(isbnNumber)
Escaping the @ sign @@ renders a single @ in the response.
In Razor, you use the 
@@foo to display the value 
of foo
Server side Comment
<%--
This is a server side 
multiline comment
--%>
@*
This is a server side 
multiline comment 
*@
Mixing expressions and text
Hello <%: title %>. <%: name %>.
Hello @title. @name.

referenced from: haacked.com

No comments:

Post a Comment