frc frc - 1 month ago 7
C# Question

Bug with if else and markup in the view

I have an

if/else
block in my view which display some html based on some
ViewBag
data, but it seems I have an error in the razor syntax (I am new to razor), I get the following msg:


Parser Error Message: 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.


relevant code:

@if (ViewBag.Sidebar == "Yes")
{
<div class="col-xs-12 col-md-3">
@Html.Action("SidebarPartial", "Pages")
</div>
<div class="col-xs-12 col-md-8">
}
else
{
<div class="col-xs-12 col-md-12">
}
@RenderBody()
</div>

Answer

You could do it like this

@if (ViewBag.Sidebar == "Yes")
{
   <div class="col-xs-12 col-md-3">
      @Html.Action("SidebarPartial", "Pages")
   </div>
      @Html.Raw("<div class='col-xs-12 col-md-8'>")
}
else
{
      @Html.Raw("<div class='col-xs-12 col-md-12'>")
}
  @RenderBody()

 </div>