C#和ASP.NET MVC:在一个视图中使用#if指令 [英] C# and ASP.NET MVC: Using #if directive in a view

查看:3035
本文介绍了C#和ASP.NET MVC:在一个视图中使用#if指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个条件编译符号,我使用所谓的释放,我在Visual Studio我的项目的属性显示。我想某些特定的CSS被应用到元素定义的符号释放的时候,我试图做到这一点从视图,但它似乎并不奏效。

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.

我的看法code看起来像这样(缩短有点用于演示目的):

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 %>

有了这个code,并与RELEASE符号集,别人的code正在运行,并且我得到的debugBanner类的div。因此,它似乎并不认为RELEASE定义。值得注意的是,在文件的.cs我实际的C#code被承认释放并运行正确的code。这是唯一的是给我这个问题的看法。

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.

没有人有任何见解呢?任何帮助将是AP preciated。谢谢你。

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);

它不工作。

推荐答案

在你的模型:

bool isRelease = false;

<% #if (RELEASE) %>
    isRelease = true;
<% #endif %>

在您的视图:

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

这篇关于C#和ASP.NET MVC:在一个视图中使用#if指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆