简体   繁体   中英

HtmlHelper inside Razor “if” statement

I have written an HtmlHelper Extension to format some content based some of our styles. The helpers render the content correctly when not enclosed within an if.

However, when I am trying to wrap them in a razor if statement, nothing is rendering, I suspect it has something to do with the Razor syntax that I am not doing correctly.

Code:

<div class="notice">

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

</div>

If I place my notificationbox outside of the if, it works fine. I have also verified that the code is dropping into the block, but none of the markup is generated in the html when I inspect it.

I've tried appening a @ like so, and ending with a colon

@Html.MyCustomNotificationBox("somparameter");

I have even tried @Html.Raw(..with the above..) which completely errors out.

Any ideas?

Have you tried putting it in text tags (those tags are not sending to client)?

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

Works Perfectly fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM