- @if
- else, else if
- @switch示例
- @for
Razor引擎在视图文件中支持所有这些控件。让我们看一些使用剃刀语法实现控制结构的示例。
@if // RazorControlStructure.cshtml
@{
ViewBag.Title = "RazorControlStructure";
var value = http://www.srcmini.com/20;
}
<
hr />
@if (value > 100)
{
<
p>This value is greater than 100.<
/p>
}
else
{ <
p>This value is less than 100.<
/p>
}
输出:
它产生以下输出。
文章图片
else, else if在else和else if语句中不需要@(at)符号。
// RazorControlStructure.cshtml
@{
Layout = null;
ViewBag.Title = "RazorControlStructure";
var value = http://www.srcmini.com/5;
}
@if (value> 5)
{
<
p>This value is greater than 5<
/p>
}
else if (value =http://www.srcmini.com/= 5)
{
<
p>This value is 5.<
/p>
}
else
{
<
p>This value is less than 5.<
/p>
}
【asp.net razor控件结构】输出:
文章图片
@switch示例// RazorControlStructure.cshtml
@{
ViewBag.Title = "RazorControlStructure";
var value = http://www.srcmini.com/20;
}
<
hr />
@switch (value)
{
case 1:
<
p>You Entered 1<
/p>
break;
case 25:
<
p>You Entered 25<
/p>
break;
default:
<
p>You entered something than 1 and 25.<
/p>
break;
}
输出:
文章图片
@for// RazorControlStructure.cshtml
@{
ViewBag.Title = "RazorControlStructure";
var value = http://www.srcmini.com/5;
}
<
hr />
<
p>This loop iterates 5 times.<
/p>
@for (var i = 0;
i <
value;
i++)
{
<
text>@i<
/text> <
br/>
}
输出:
它产生以下输出。
文章图片
推荐阅读
- asp.net razor html助手
- asp.net razor代码块
- asp.net razor代码表达式
- asp.net razor标记语法
- asp.net mvc ViewData、ViewBag和TempData
- asp.net mvc脚手架
- asp.net mvc验证
- asp.net mvc路由
- asp.net mvc boostrap框架