HOW TO USE #IF DIRECTIVE IN A VIEW IN MVC?

Pawan Shukla

Total Post:38

Points:274
Posted by  Pawan Shukla
 1089  View(s)
Ratings:
Rate this:
I've got a conditional compilation symbol I'm using called "RELEASE", that I indicated in my project's properties in Visual Studio. I want some particular CSS to be applied to elements when the RELEASE symbol is defined, and I was trying to do that from the view, but it doesn't seem to be working.

My view code looks like this (shortened a bit for demo purposes):

<% #if (RELEASE) %>
    <div class="releaseBanner">Banner text here</div>
<% #else %>
    <div class="debugBanner">Banner text here</div>
<% #endif %>
With this code, and with the RELEASE symbol set, the 'else' code is running and I'm getting a div with the debugBanner class. So it doesn't seem to think that RELEASE is defined. It's worth noting that my actual C# code in .cs files is recognizing RELEASE and runs the correct code. It's only the view that is giving me the problem.

Does anyone have any insight into this? Any help would be appreciated. Thanks.

Clarification: I should have mentioned that this view is already a partial view, and I'll simply render it in pages where I need it. That's because these banners will be on certain pages and not others. So even when rendering it as a partial view via:

Html.RenderPartial("BannerView");

it's not working.
  1. Kamlakar Singh

    Post:194

    Points:1396
    Re: How to use #if directive in a view in mvc?

    In your model:

    bool isRelease = false;

    <% #if (RELEASE) %>
        isRelease = true;
    <% #endif %>
    In your view:

    <% if (Model.isRelease) { %>
        <div class="releaseBanner">Banner text here</div>
    <% } else { %>
        <div class="debugBanner">Banner text here</div>
    <% } %>

  1. jacob rasel

    Post:88

    Points:616
    Re: How to use #if directive in a view in mvc?

    You can use this for access all views


    public static bool IsReleaseBuild(this HtmlHelper helper)

    {

         #if DEBUG

                 return false;

         #else

                 return true;

         #endif

     }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!