MVC Razor 語法(轉)

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   使用   

http://blog.csdn.net/pasic/article/details/7072340

原文地址:MVC Razor 文法(轉)panzhaowen_jacki
文法名稱 Razor 語法 Web Forms 等效文法
代碼塊
 @{ int x = 123; string y = "because."; } 
 <% int x = 123; string y = "because."; %> 
運算式(預設encode)
 <span>@model.Message</span> 
 <span><%: model.Message %></span> 
運算式(不encode)
 <span> @Html.Raw(model.Message) </span> 
 <span><%= model.Message %></span> 
結合文本和標記的迴圈
 @foreach(var item in items) { <span>@item.Prop</span> } 
 <% foreach(var item in items) { %> <span><%: item.Prop %></span> <% } %> 
代碼和文本混合
 @if (foo) { <text>Plain Text</text> } 
 <% if (foo) { %> Plain Text <% } %> 
代碼和文本混合
 @if (foo) { @:Plain Text is @bar } 
同上
Email 地址
 Hi [email protected]
Razor 認識基本的郵件格式.可智能識別.
顯示運算式
 <span>[email protected](isbnNumber)</span> 
在括弧裡可以有些簡單的操作.擴充一下就是@(20*pageIndex) 輸出運算結果
 輸出@符號 
 <span>In Razor, you use the @@foo to display the value of foo</span> 
要顯示@符號,用兩個@符號"@@"表示.
伺服器端注釋
 @* This is a server side multiline comment  *@ 
 <%-- This is a server side multiline comment --%> 
調用一個方法
 @(MyClass.MyMethod<AType>()) 
使用括弧來明確表達是什麼.
建立一個Razor委託
 @{ Func<dynamic, object> b = @<strong>@item</strong>; } @b("Bold this")
更多資訊查看 this blog post .
混合運算式和文本
 Hello @title. @name.
 Hello <%: title %>. <%: name %>.

 

希望對您有所協助. 

 

補充一個在View的指令碼Script中顯示JSON對象的方法

需求:var data=[{id:1,title="標題1},{id:2,title="標題2"}] 

實現:var [email protected](@Newtonsoft.Json.JavaScriptConvert.SerializeObject(Model)) 

用Json.Net轉換一下再Raw輸出即可

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.