ASP.NET MVC3細嚼慢嚥---(3)Razor視圖語法

MVC3.0中新增長了Razor視圖,Razor視圖的語法以@符號爲核心,貌似在這個時代離不開@了,微博,郵箱都用這個。html

 1.輸出變量和文本ide

[html]  view plain copy
 
  1. @DateTime.Now  
[html]  view plain copy
 
  1. @for (int i = 0; i < 5; i++)  
  2. {  
  3.    <p>@i</p>  
  4. }  


 2.HTML標籤編碼編碼

  默認狀況下是對HTML標籤編碼的。htm

  使用HTML.RAW對HTML標籤不進行編碼string

[html]  view plain copy
 
  1. @{  
  2.     string str = "Hello <br/>Word";  
  3.     @str  
  4.     @Html.Raw(str)  
  5.     }  


 

3.註釋it

 使用@* 註釋內容  *@符號進行註釋微博

[html]  view plain copy
 
  1. @*  
  2. //返回方法返回值  
  3. *@  


 

4.單行輸出class

使用@:進行單行輸出, 也可使用 text標記進行單行輸出變量

[html]  view plain copy
 
  1. @{       
  2.   @: Hello  
  3.   @: world    
  4.   }  
  5.   
  6.   <text>   
  7.   Hello  
  8.   world  
  9.   </text>  


 5.@前無空格輸出變量語法

若是@前無空格輸出變量,可使用@()進行輸出

[html]  view plain copy
 
  1. <p>Hello@(DateTime.Now)</p>  

 

6.在頁面中輸出@符號

若是想在頁面中輸出@符號,可用兩個@符號來代替

[html]  view plain copy
 
  1. <p>Hello@@@(DateTime.Now)</p>  



 

相關文章
相關標籤/搜索