foreach 循环:
@{
Dim names As New List(Of String) From {"Alice", "Bob", "Charlie"}
}
<ul>
@For Each name As String In names
<li>@name</li>
Next
</ul>
for 循环:
<ul>
@For i As Integer = 1 To 5
<li>Item @i</li>
Next
</ul>
while 循环:
@{
Dim count As Integer = 0
}
<ul>
@While count < 3
<li>Item @count</li>
count += 1
End While
</ul>
嵌套循环:
@{
Dim departments As New List(Of String) From {"HR", "IT", "Finance"}
Dim employees As New Dictionary(Of String, List(Of String)) From {
{"HR", New List(Of String) From {"Alice", "Bob"}},
{"IT", New List(Of String) From {"Charlie", "David"}},
{"Finance", New List(Of String) From {"Eve", "Frank"}}
}
}
<ul>
@For Each department As String In departments
<li>@department
<ul>
@For Each employee As String In employees(department)
<li>@employee</li>
Next
</ul>
</li>
Next
</ul>
在这些示例中,VB语法被用于创建和使用变量以及在HTML中生成动态内容。你可以根据自己的偏好或项目要求选择使用C#或VB语法。在Razor中,C#和VB的语法在使用上是相似的,因此你可以根据自己的熟悉程度或团队的技术栈来选择其中之一。
转载请注明出处:http://www.zyzy.cn/article/detail/14706/ASP.NET