0

When I wrote the if and else statement in a foreach loop, I have encountered an error. I'm not sure what went wrong. I even added @if and @var{ }; to the code. But there is still error.

10
  • What's the error? Commented Oct 25, 2018 at 0:49
  • it stated was the '}' Commented Oct 25, 2018 at 0:53
  • The parser is confused when you don't close the tags when you inserted the else block in the middle of your html. What are you trying to do with that conditional block? Try to write out two complete and valid html snippets instead of mixing C# code with html. Commented Oct 25, 2018 at 1:06
  • @Jasen can you show me where is it? But I've already closed it. Commented Oct 25, 2018 at 1:11
  • Remove all your html from the for loop. Then slowly introduce it back. Your <a> and parent <div> are not closed. Commented Oct 25, 2018 at 1:13

1 Answer 1

2

It's not a good idea to mix partial html fragments with conditional C#/Razor code. The code might repeat but it's far easier to read and maintain later.

I may not have captured your intent correctly, but I can read this and understand how the condition affects the output.

@foreach (var item in Model)
{
    var HeaderImage = item.HeaderPath;

    if (image != "")
    {
        var fileSavePath = Path.Combine(@"\\mainPage.com", "");
        <div class="picGallery">
            <img src="@Url.Content(item.HeaderPath)" alt="Image" />

            <a href="@Url.Action("ViewPage", "Home")">
            </a>
            <div class="desc2">
                <p style="font-size: 13;">@item.Text</p>
                <p style="font-size: 13;">@item.text_2</p>
            </div>
        </div>
    }
    else
    {
        <div class="picGallery">
            <img src="@Url.Content(item.HeaderPath)" alt="Image" />

            <a href="@Url.Action("ViewPage", "Home")">
                <div id="scanIcon">
                    <img src=img.png alt="imageView" />
                    <h1 style="font-size: 10px;">View Image</h1>
                </div>
            </a>
            <div class="desc2">
                <p style="font-size: 13;">@item.Text</p>
                <p style="font-size: 13;">@item.text_2</p>
            </div>
        </div>
    }
}

You can shorten this. But you need to be careful to escape the @if the nested blocks of code within html. It also becomes more difficult to understand what the output should be.

@foreach (var item in Model)
{
    var HeaderImage = item.HeaderPath;

    <div class="picGallery">
        <img src="@Url.Content(item.HeaderPath)" alt="Image" />

        <a href="@Url.Action("ViewPage", "Home")">
            @if (image != "")
            {
                // image exists
                var fileSavePath = Path.Combine(@"\\mainPage.com", "");

                <div>  </div>
            }
            else
            {
                <div id="scanIcon">
                    <img src=img.png alt="imageView" />
                    <h1 style="font-size: 10px;">View Image</h1>
                </div>
            }
        </a>
        <div class="desc2">
            <p style="font-size: 13;">@item.Text</p>
        </div>
    </div>
}
Sign up to request clarification or add additional context in comments.

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.