0

I'm working on an ASP.NET MVC app that uses Razor. Part of the HTML is rendered via a loop. I need to conditionally create an opening/closing element. For some reason, the view renderer throws a runtime error when i try the following:

  <div id="content">
    <div id="banner">@ViewBag.BannerText</div></br>

    <div id="group">
@{ 
  int currentID = 0;
  foreach (Item item in ViewBag.Items) {
  if (currentID != item.ID) {
      <div>
        <h3>@item.GetItemTitle()</h3>    
        <div>@item.Name</div>
  }

  if (currentID != item.ID) {
    @Html.Raw("</div>");
    currentID = item.ID;   
  }
}
</div>
</div>

Everything looks correct to me. However, I can't figure out why it won't work. It seems like everytime I had the @Html.Raw("") it fails. However, if I remove the conditional if-statement and the @Html.Raw(""); it works fine.

3
  • What runtime error. Supply details. Commented Jun 3, 2013 at 20:51
  • Why do you need @Html.Raw? Does @:</div> not work? Commented Jun 3, 2013 at 20:56
  • Tried @:</div>, however that did not work. The runtime error I am getting is: 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. Commented Jun 3, 2013 at 22:17

1 Answer 1

1

The problem is your closing DIV must be in the same block as the opening DIV. Use @Html.Raw on your opening DIV and your problem should go away.

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.