Eri*_*rik 8 razor asp.net-mvc-3
我在else块中显示(而不是显示)纯文本时遇到问题.
if (Model.CareerFields != null && ViewBag.CFCount > 0)
{
<h3>Careerfields Listing</h3>
<table>
<tr>
<th></th>
<th>Careerfield Name</th>
</tr>
@foreach (var item in Model.CareerFields)
{
<tr>
<td>
@Html.ActionLink("Select", "Index", new { careerFieldID = item.CareerFieldId })
</td>
<td>
@item.CareerFieldName
</td>
</tr>
}
</table>
}
else
{
No Careerfields associated with @ViewBag.SelectedDivisionTitle
}
Run Code Online (Sandbox Code Playgroud)
if块工作正常.文本仅在呈现时呈现.但是,else块文本在页面加载时呈现,而不是仅在其计算结果为false时呈现.
我试过用了
Hmtl.Raw("No Careerfields associated with ")
<text>No Careerfields associated with @ViewBag.SelectedDivisionTitle</text>
@:No Careerfields associated with @ViewBag.SelectedDivisionTitle
Run Code Online (Sandbox Code Playgroud)
但它仍然在评估之前呈现明文.
有什么建议?
将您的"纯文本"放在裸<span>标记内:
else
{
<span>No Careerfields associated with @ViewBag.SelectedDivisionTitle</span>
}
Run Code Online (Sandbox Code Playgroud)
浏览器不应该使它特殊(除非你有css选择每个跨度),它将帮助剃刀感知C#的结尾并打印你的HTML.
最简洁、最正确的答案是:
@:文本前面。(注意:后面的@)
@这仍然允许通过在变量名称前面添加 an 来在文本中嵌入变量:
@if (someCondition)
{
@:Some text you want to see.
}
else
{
@:Some other text, with a variable @someVariable included in the text.
}
Run Code Online (Sandbox Code Playgroud)
以下代码对我来说非常合适:
@if (false) {
<h3>
Careerfields Listing
</h3>
<table>
<tr>
<th>
</th>
<th>
Careerfield Name
</th>
</tr>
</table>
}
else
{
@:No Careerfields associated with @ViewBag.SelectedDivisionTitle
}
Run Code Online (Sandbox Code Playgroud)
您可以看到在将条件更改为true时呈现if的内容.
| 归档时间: |
|
| 查看次数: |
19762 次 |
| 最近记录: |