Asp.net mvc 3 MVC 3 RAZOR编码逻辑以文本形式呈现,即使它们在@{}
以下代码行有什么问题Asp.net mvc 3 MVC 3 RAZOR编码逻辑以文本形式呈现,即使它们在@{},asp.net-mvc-3,razor,razor-declarative-helpers,Asp.net Mvc 3,Razor,Razor Declarative Helpers,以下代码行有什么问题 <table> @{ string subGroup = ""; foreach (var item in Model) { if (item.SubGroupName != subGroup) { subGroup = item.SubGroupName;
<table>
@{
string subGroup = "";
foreach (var item in Model)
{
if (item.SubGroupName != subGroup)
{
subGroup = item.SubGroupName;
<tr style="background-color: #C1E0FF; text-align: left">
<td colspan="2">
@item.SubGroupName;
</td>
</tr>
}
<tr>
<td>
@Html.DisplayFor(modelItem => item.configurationitemkey)
</td>
<td>
if (item.IsBoolean)
@Html.DisplayFor(modelItem => item.IsAvailable)
else
@Html.DisplayFor(modelItem => item.ConfigurationItemValue)
</td>
</tr>
}
}
</table>
以下代码行即使在@{}内也作为测试呈现
<td>
if (item.IsBoolean)
@Html.DisplayFor(modelItem => item.IsAvailable)
else
@Html.DisplayFor(modelItem => item.ConfigurationItemValue)
</td>
指导我在MVC中实现这种编程逻辑的最佳方法
谢谢
@Paul您是否尝试在它们前面加上@,因为它们前面的标记意味着Razor解析器已切换回HTML上下文,而不是服务器端代码:
@{
string subGroup = "";
}
<table>
@foreach (var item in Model)
{
if (item.SubGroupName != subGroup)
{
subGroup = item.SubGroupName;
}
<tr style="background-color: #C1E0FF; text-align: left">
<td colspan="2">
@item.SubGroupName;
</td>
</tr>
<tr>
<td>
@Html.DisplayFor(modelItem => item.configurationitemkey)
</td>
<td>
@if (item.IsBoolean)
{
@Html.DisplayFor(modelItem => item.IsAvailable)
}
else
{
@Html.DisplayFor(modelItem => item.ConfigurationItemValue)
}
</td>
</tr>
}
</table>
备注:在您展示的示例中,代码中似乎没有使用subGroup变量,但我还是将其包括在内,因为这可能只是实际代码的简化版本,您可能需要它。谢谢。是的,我试过了,但如果结果显示为空白页面,请在前面加上@OK。我现在就试试。顺便说一下,我在if item.subGroup name!=subGroup{subGroup=item.subGroup;}谢谢,我已经工作了。但是现在[如果item.subGroup!=subGroup{subGroup=item.subGroup;}]不起作用。实际上,我不想重复相同的子组。完成。谢谢这次我忘了在if条件>>>