简体   繁体   English

ASP.NET MVC - 简单如果其他

[英]ASP.NET MVC - Simple If Else

For some reason I can't seem to get my head around why I get a 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. 出于某种原因,我似乎无法理解为什么我得到一个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. 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. error in my ASP.NET MVC Application. 我的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;">
    }
    ....
    ....

Prior to the above, I was simply doing: <div style="float: left; height: 200px; padding-right: 5px;"> , however I am in need of this If Else to make it look better. 在上面之前,我只是这样做: <div style="float: left; height: 200px; padding-right: 5px;"> ,但是我需要这个,如果有的话,让它看起来更好。

What am I doing wrong in the If Else statement? 在If Else声明中,我做错了什么?

Your problem is that the MVC parser is interpreting your code as you having left an open <div> sitting around. 你的问题是MVC解析器正在解释你的代码,因为你已经离开了一个开放的<div> Rather than opening two, try reworking your code and outputting just one: 而不是打开两个,尝试重新编写代码并输出一个:

@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>
}

It could also be done in an even more compact manner: 它也可以以更紧凑的方式完成:

@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>
}

This should work: 这应该工作:

@foreach (var image in Model.Images)
{
    if (counter == Model.Images.Count - 1)
    {
        <text><div style="float: left; height: 250px; padding-right: 5px;"></text>
    }
    else
    {
        <text><div style="float: left; height: 200px; padding-right: 5px;"></text>
    }
}

我会尝试完全替换if / else:

 <div style="@(counter == Model.Images.Count - 1 ? "float: left; height: 250px; padding-right: 5px;" : "float: left; height: 200px; padding-right: 5px;")">

Try wrapping the whole thing in @{ ... }. 尝试将整个内容包装在@ {...}中。 And remove the @ symbols from the existing syntax. 并从现有语法中删除@符号。

检查是否需要来自foreach @并尝试在每个div前面附加@:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM