出于某种原因,我似乎无法理解为什么我会收到 The if 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.。我的 ASP.NET MVC 应用程序出错。
@foreach (var image in Model.Images)
{
if (counter == Model.Images.Count - 1)
{
<div style="float: left; height: 250px; padding-right: 5px;">
}
else
{
<div style="float: left; height: 200px; padding-right: 5px;">
}
....
....
在上述之前,我只是在做:<div style="float: left; height: 200px; padding-right: 5px;"> ,但是我需要这个 If Else 来让它看起来更好。
我在 If Else 语句中做错了什么?
请您参考如下方法:
您的问题是 MVC 解析器在您打开 <div> 时解释您的代码围坐。与其打开两个,不如尝试重新编写代码并只输出一个:
@foreach (var image in Model.Images)
{
int height;
if (counter == Model.Images.Count - 1)
{
height = 250;
}
else
{
height = 200;
}
<div style="float: left; height: @(height.ToString()+"px"); padding-right: 5px;">
...
...
</div>
}
它也可以以更紧凑的方式完成:
@foreach (var image in Model.Images)
{
bool condition = (counter == Model.Images.Count - 1)
<div style="float: left; height: @( condition ? "200px" : "250px"); padding-right: 5px;">
...
...
</div>
}
