How to include HTML inside an If statement

Why, when I have certain HTML in my C # statements, does Razor complain about invalid expressions and terms?

And how can I fix this?

For instance; in my _layout.cshtml:

@if (Team.Id == ViewBag.TeamId) { </div> <div class="row"> &nbsp; } 
+5
source share
2 answers

The razor is really trying to understand your HTML. Unfortunately, it cannot be very intelligent and recognize dynamic code or code outside of an expression.

WITH

 </div> <div> 

is invalid HTML, it complains.

You can avoid this problem by using @:

This forces the razor to bypass its "HTML recognition" and simply print whatever you ask.

eg.

 @if (Team.Id == ViewBag.TeamId) { @:</div> @:<div class="row"> @:&nbsp; } 
+8
source

You can also use @Html.Raw("YOUR HTML"); inside an if to add HTML

0
source

All Articles