1

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. error in my ASP.NET MVC Application.

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

What am I doing wrong in the If Else statement?

2
  • 3
    u must use @ first time. so dont use @ again before else Commented Jul 16, 2013 at 10:34
  • I tried that before too but it doesn't work. Commented Jul 16, 2013 at 10:39

5 Answers 5

2

Your problem is that the MVC parser is interpreting your code as you having left an open <div> sitting around. 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>
}
Sign up to request clarification or add additional context in comments.

1 Comment

I think this is right. I spent 30 minutes scratching my head over the same issue. html tags must be closed within braces. It is annoying that the error message is not very descriptive.
1

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

1 Comment

I have updated my answer, could you check to see if it works now?
1

I would try to replace if/else completely like that:

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

1 Comment

I used this approach and extracted the style and placed it in a .css files.
0

Try wrapping the whole thing in @{ ... }. And remove the @ symbols from the existing syntax.

Comments

0

Check if @ from foreach is needed and also try to append @: in front of every div

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.