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

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

问题描述

我使用了一个名为RELEASE"的条件编译符号,我在 Visual Studio 的项目属性中指出了它.我希望在定义 RELEASE 符号时将一些特定的 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.

我的视图代码如下所示(为了演示目的,稍微缩短了一点):

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

使用此代码和 RELEASE 符号集,'else' 代码正在运行,我得到一个带有 debugBanner 类的 div.所以似乎没有定义 RELEASE .值得注意的是,我在 .cs 文件中的实际 C# 代码正在识别 RELEASE 并运行正确的代码.只是观点给我带来了问题.

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

它不工作.

推荐答案

在你的模型中:

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天全站免登陆