这不使用与 OP 相同的代码,但我将一些 C# 代码从 MVC 书籍转换为 VB.NET,并且被混合内联 HTML 和 VB 代码卡住了。这是原来的 C#:
@using (Html.BeginForm()) {
@Html.ValidationSummary()
<p>Your name: @Html.TextBoxFor(x => x.Name, new { @class = "form-control" }) </p>
<p>Your email: @Html.TextBoxFor(x => x.Email, new { @class = "form-control" }) </p>
<p>Your phone: @Html.TextBoxFor(x => x.Phone, new { @class = "form-control" }) </p>
<p>Will you attend?
@Html.DropDownListFor(x => x.WillAttend, new[] {
new SelectListItem() {Text = "Yes, I'll be there",
Value = bool.TrueString},
new SelectListItem() {Text = "No, I can't come",
Value = bool.FalseString}
}, "Choose an option", new { @class = "form-control" })
</p>
}
这些是在 VB 中表示它的不同方式:
@Using Html.BeginForm()
@:<p>Your name: @Html.TextBoxFor(Function(x) x.Name)</p>
@:<p>Your email: @Html.TextBoxFor(Function(x) x.Email)</p>
@:<p>Your phone: @Html.TextBoxFor(Function(x) x.Phone)</p>
@:<p>Will you attend?
@Html.DropDownListFor(Function(x) x.WillAttend, New SelectListItem() {New SelectListItem() With {.Text = "Yes", .Value = Boolean.TrueString}, New SelectListItem() With {.Text = "No", .Value = Boolean.FalseString}})
@:</p>
End Using
@Using Html.BeginForm()
@<text>
<p>Your name: @Html.TextBoxFor(Function(x) x.Name)</p>
<p>Your email: @Html.TextBoxFor(Function(x) x.Email)</p>
<p>Your phone: @Html.TextBoxFor(Function(x) x.Phone)</p>
<p>Will you attend?
@Html.DropDownListFor(Function(x) x.WillAttend, New SelectListItem() {New SelectListItem() With {.Text = "Yes", .Value = Boolean.TrueString}, New SelectListItem() With {.Text = "No", .Value = Boolean.FalseString}})
</p>
</text>
End Using
@code
Using Html.BeginForm()
@:<p>Your name: @Html.TextBoxFor(Function(x) x.Name)</p>
@:<p>Your email: @Html.TextBoxFor(Function(x) x.Email)</p>
@:<p>Your phone: @Html.TextBoxFor(Function(x) x.Phone)</p>
@:<p>Will you attend?
@Html.DropDownListFor(Function(x) x.WillAttend, New SelectListItem() {New SelectListItem() With {.Text = "Yes", .Value = Boolean.TrueString}, New SelectListItem() With {.Text = "No", .Value = Boolean.FalseString}})
@:</p>
End Using
End Code
@code
Using Html.BeginForm()
@<text>
<p>Your name: @Html.TextBoxFor(Function(x) x.Name)</p>
<p>Your email: @Html.TextBoxFor(Function(x) x.Email)</p>
<p>Your phone: @Html.TextBoxFor(Function(x) x.Phone)</p>
<p>Will you attend?
@Html.DropDownListFor(Function(x) x.WillAttend, New SelectListItem() {New SelectListItem() With {.Text = "Yes", .Value = Boolean.TrueString}, New SelectListItem() With {.Text = "No", .Value = Boolean.FalseString}})
</p>
</text>
End Using
End Code
从中显而易见的是,当您需要在代码块中包含内联 HTML 时,您需要在每行前面加上 @: 前缀,或者在 HTML 后面加上 @<text></text> 块。很明显,当您使用 @Code ... End Code 而不是使用 @ 开始代码块时,这也适用。
附言请注意,@<text></text> 标签不会输出到页面,因此它们不会干扰任何内容