【问题标题】:How to use C# variables declared in View page inside a jsRender template如何使用在 jsRender 模板内的视图页面中声明的 C# 变量
【发布时间】:2017-05-16 16:55:24
【问题描述】:

我需要使用在 js 渲染模板内的 cshtml 视图页面中声明的变量。可能吗??下面是我的代码。

@{
bool isAdmin = false;
}

<script type="text/x-jsrender" id="ProjectStatus">
{{if Status == 1 && isAdmin}}
<span class="status status-open">{{:Status}}</span>
{{/if}}
</script>

提前致谢。

Dinesh。

【问题讨论】:

    标签: javascript c# razor jsrender


    【解决方案1】:

    要在 Javascript 中使用 C# 变量,只需添加 @ :

    {{if Status == 1 && '@isAdmin' == 'True'}}
         <span class="status status-open">{{:Status}}</span>
    {{/if}}
    

    【讨论】:

    • 感谢您的帮助。我尝试使用@isAdmin,但错过了“== True”。那么 '@isAdmin' == 'True' 工作正常。 :) :)
    猜你喜欢
    • 1970-01-01
    • 2018-08-04
    • 2016-11-29
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 2020-05-10
    相关资源
    最近更新 更多