發(fā)表日期:2019-04 文章編輯:小燈 瀏覽次數(shù):2750
Razor邏輯:執(zhí)行基于條件的代碼。
允許您執(zhí)行基于條件的代碼。
如需測試某個條件,您可以使用 if 語句。if 語句會基于您的測試來返回 true 或 false:
if 語句啟動代碼塊
條件位于括號中
如果條件為真,則執(zhí)行花括號中的代碼
@{var?price=50;} <html> <body> @if?(price>30) ????{ ????<p>The?price?is?too?high.</p> ????} </body> </html>
運行實例
if 語句能夠包含 else 條件。
else 條件定義條件為 false 時執(zhí)行的代碼。
@{var?price=20;} <html> <body> @if?(price>30) ??{ ??<p>The?price?is?too?high.</p> ??} else ??{ ??<p>The?price?is?OK.</p> ??}? </body> </html>
運行實例
注釋:在上面的例子中,如果價格不大于 30,則執(zhí)行其余的語句。
可通過 else if 條件來測試多個條件:
@{var?price=25;} <html> <body> @if?(price>=30) ??{ ??<p>The?price?is?high.</p> ??} else?if?(price>20?&&?price<30)? ??{ ??<p>The?price?is?OK.</p> ??} else ??{ ??<p>The?price?is?low.</p> ??}???? </body> </html>
運行實例
在上面的例子中,如果第一個條件為 true,則執(zhí)行第一個代碼塊。
否則,如果下一個條件為 true,則執(zhí)行第二個代碼塊。
您能夠設置任意數(shù)量的 else if 條件。
如果 if 和 else if 條件均不為 true,則執(zhí)行最后一個 else 代碼塊。
switch 代碼塊可用于測試一系列具體的條件:
@{ var?weekday=DateTime.Now.DayOfWeek; var?day=weekday.ToString(); var?message=""; } <html> <body> @switch(day) { case?"Monday": ????message="This?is?the?first?weekday."; ????break; case?"Thursday": ????message="Only?one?day?before?weekend."; ????break; case?"Friday": ????message="Tomorrow?is?weekend!"; ????break; default: ????message="Today?is?"?+?day; ????break; } <p>@message</p> </body> </html>
日期:2019-04 瀏覽次數(shù):3145
日期:2019-04 瀏覽次數(shù):3120
日期:2019-04 瀏覽次數(shù):3029
日期:2019-04 瀏覽次數(shù):2856
日期:2019-04 瀏覽次數(shù):2946
日期:2019-04 瀏覽次數(shù):3112
日期:2019-04 瀏覽次數(shù):3048
日期:2019-04 瀏覽次數(shù):3310
日期:2019-04 瀏覽次數(shù):3361
日期:2019-04 瀏覽次數(shù):3013
日期:2019-04 瀏覽次數(shù):3390
日期:2019-04 瀏覽次數(shù):2811
日期:2019-04 瀏覽次數(shù):2955
日期:2019-04 瀏覽次數(shù):3072
日期:2019-04 瀏覽次數(shù):2923
日期:2019-04 瀏覽次數(shù):3367
日期:2019-04 瀏覽次數(shù):3267
日期:2019-04 瀏覽次數(shù):2947
日期:2019-04 瀏覽次數(shù):3171
日期:2019-04 瀏覽次數(shù):3005
日期:2019-04 瀏覽次數(shù):2776
日期:2019-04 瀏覽次數(shù):3083
日期:2019-04 瀏覽次數(shù):2972
日期:2019-04 瀏覽次數(shù):3072
日期:2019-04 瀏覽次數(shù):3023
日期:2019-04 瀏覽次數(shù):2895
日期:2019-04 瀏覽次數(shù):2749
日期:2019-04 瀏覽次數(shù):2766
日期:2019-04 瀏覽次數(shù):2828
日期:2019-04 瀏覽次數(shù):2853
Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.