7

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?

4

2 回答 2

19

您是否尝试将其放入text标签中(这些标签未发送给客户端)?

@if (DataModel.UserHasExpired)
{
    <text>@Html.MyCustomNotificationBox("somparameter")</text>
}
于 2012-12-10T21:38:04.967 回答
8
<p>
   @if (true)
   {
       @Html.Hello("World")
   }
</p>

完美无缺

于 2012-12-10T21:56:03.847 回答