<ul> @foreach(var p in products){ <li> @p.ProductName @if(p.unitsInStock==0){ @:(Out of stock) } else if(p.unitsInStock<4){ @:(only @p.unitsInStock Left!) } </li> } </ul> (14)多行內(nèi)容的混合代碼 1)內(nèi)容在html標記閉合中時 復(fù)制代碼 代碼如下:
@if(p.unitsInStock==0){ <p> Line one of Content Line two of Content Date is: @DateTime.Now Line four of Content </p> } (15)多行內(nèi)容混合代碼 1)內(nèi)容外部沒有html標記包裝時 1)@if(p.unitsInStock==0){ @:Line one of Content @:Line two of Content @:Line four of Content } 2)@if(p.unitsInStock==0){ <text> Line one of Content Line two of Content Date is: @DateTime.Now Line four of Content </text> } 為什么需要布局頁面 (1) 沒有使用布局頁時,每個頁面中將大量的重復(fù)我們的核心網(wǎng)站布局代碼 1)代碼冗余 2)不利于管理 3)不利于后期的修改和維護 Razor的布局 (1) 不需要使用專門的.master文件,而統(tǒng)一使用.cshtml(VB中為.vbhtml)文件 1)布局文件名通常采用類似_Layout.cshtml的名字 (2)@RenderBody()用于標識布局頁中可替換內(nèi)容的主題部分 (3)內(nèi)容頁中通過給頁面的Layout屬性賦值實現(xiàn)指定布局(模版)頁的文件路徑 復(fù)制代碼 代碼如下: