我正在使用ASP.NET MVC 3.我的剃刀视图中有以下代码:
@foreach(var c in Model.ChildCategories) {
<div>@c.Name<br></div>
}
效果很好。为什么当我拿走内在的div标签时它不起作用?这就是我试过的:
@foreach(var c in Model.ChildCategories) {
@c.Name<br>
}
我没有关闭break标记,因为我使用的是严格的doc类型:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
我想查看视图时收到的错误是:
The foreach block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.
我不想使用内部div标签,因为我不想在标记中添加不必要的标签。
我还有以下代码:
@if (Model.ParentCategory != null)
{
@Model.ParentCategory.Name
}
else
{
@:N/A
}
我想显示父类别的名称,或只显示纯文本N / A.我做得对吗?将大括号放在if或旁边的大括号(如if (...) {
?
答案 0 :(得分:1)
查看Phil Haack的post razor on syntax
@foreach(var c in Model.ChildCategories)
{
<text>
@c.Name<br />
</text>
}
但不要使用<br />
而是考虑使用<p>
@foreach(var c in Model.ChildCategories)
{
<p>
@c.Name
</p>
}
@:N/A
的最后一位是正确的。
答案 1 :(得分:0)
试试这个〜
@foreach(var c in Model.ChildCategories) {
<div>@{@c.Name}<br></div>
}