HtmlHelper inside the phrase "if"

I wrote the HtmlHelper extension to format some content based on some of our styles. Helpers display content correctly if they are not enclosed inside an if.

However, when I try to wrap them in an if razor operator, nothing is displayed, I suspect this has something to do with Razor syntax, which I am not doing right.

code:

<div class="notice"> @if (DataModel.UserHasExpired) { Html.MyCustomNotificationBox("someparameter") // My helper Should render a div } </div> 

If I put my notification outside the if field, it works fine. I also checked that the code falls into the block, but none of the markup is generated in html when I check it.

I tried to attach @ so, and ending with a colon

 @Html.MyCustomNotificationBox("somparameter"); 

I even tried @ Html.Raw (.. with the above ..), which doesn't work completely.

Any ideas?

+4
source share
2 answers

Have you tried to put it in text tags (those tags do not send to the client)?

 @if (DataModel.UserHasExpired) { <text>@Html.MyCustomNotificationBox("somparameter")</text> } 
+19
source
 <p> @if (true) { @Html.Hello("World") } </p> 

Works great

+6
source

All Articles