單行:前面加上@,例:@Datetime.Now
多行:用@{},例:
@{
你的程式碼
}
內容Contet Mode:
單行:前面加上@:,例:@:你好嗎
多行:用<Text></Text>,例:
@{
var id = Request["id"];
<Text> 我的ID是@id <Text>
}
轉型:
AsBool(), AsDateTime(), AsDecimal(), AsFloat(), and AsInt()
C#寫法
var itemName = ( GetOrderItemName<string>(item) );
可否轉為該型別:
IsBool(), IsDateTime(), IsDecimal(), IsFloat(), and IsInt()
魔法數字-1
例如:
var id = Request["id"].IsInt(-1); // Use -1 as a "magic number"
if(id != -1) { /* ... */ }
註解:
@* 這裡面是註解 *%
驗証函式:
ModelState
@Html.ValidationSummary() ==> css style : validation-summary-errors
@Html.ValidationMessage() ==> css style : "field-validation-error"
資料庫:
執行:
@{
var title = Request["Title"];
var summary = Request["Summary"];
var body = Request["Body"];
if(IsPost)
{
Database
.Open("Blog")
.Execute("INSERT INTO Posts (Title, Summary, Body) " +
"VALUES(@0,@1,@2)",
title, summary, body);
}
}
顯示:
@{
var db = Database.Open("Blog");
var posts = db.Query("SELECT ID,Title,Body FROM Posts");
}
@foreach(var post in posts) {
<div>
<h3>@post.Title</h3>
<div>@post.Body</div>
</div>
}
函式:
if ~
@if(User.IsAuthenticated) {<span>Hello, @User.Username!</span>} else {<span>Please @Html.ActionLink("log in")</span>}
foreach ~
@foreach( var post in blogPosts) {<a href="@post.Href">@post.Title</a>}
語法轉譯:
@ = @
@Html.raw() : 將@轉成 @
javascript中使用@Ajax.JavascriptStringEncode()