【问题标题】:Handlebars inside MVC Razor viewMVC Razor 视图中的把手
【发布时间】:2018-09-04 00:48:42
【问题描述】:

我在 ASP.NET MVC 应用程序的 razor 视图中使用把手。

我有一种情况,我必须在剃须刀的 else 条件中使用车把的 if 条件,如下所示。

Index.cshtml

<script id="template" type="text/x-handlebars-template">
       ....
      @if (CurrentUserRepository.IsInRoleEnums(RoleEnum.TPS_Administrator)) 
           {
               <i class="fas fa-trash-alt text-dark pointer" title="Delete this item" onclick="DeleteNotification({{NotificationId}})"></i>
           }
       else
           {
              //handlebars shown below doesn't work
              {{#if UserCanSetClosed}}
                 <i class="fas fa-trash-alt text-dark pointer" title="Delete this item" onclick="DeleteNotification({{NotificationId}})"></i>
              {{/if}}                    
           }
</script>

我知道一种替代方法是获取传递给把手的 json 中条件所需的所有值,并将其与模板中的把手条件一起使用。但我想尽可能使用 Razor。

有什么办法可以做到吗?

注意:我尝试在存储库中搜索,但这个问题似乎不是重复的!

【问题讨论】:

  • 你现在做的有什么问题?
  • Razor 视图无法编译。 {{#if UserCanSetClosed}} 行中的“#”被视为预处理器指令。准确地说,else 部分中的代码被视为 C# 代码而不是把手。

标签: c# .net asp.net-mvc razor handlebars.js


【解决方案1】:

在 Razor 中使用 &lt;text&gt; 标记可能会帮助您让视图引擎正确处理把手语法。

试试这个示例代码:

<script id="template" type="text/x-handlebars-template">

  @if (CurrentUserRepository.IsInRoleEnums(RoleEnum.TPS_Administrator)) 
  {
    <text><i class="fas fa-trash-alt text-dark pointer" title="Delete this item" onclick="DeleteNotification({{NotificationId }})"></i></text>
  }
  else
  {
      //handlebars shown below doesn't work
          <text>{{#if UserCanSetClosed}}</text>
          <text><i class="fas fa-trash-alt text-dark pointer" title="Delete this item" onclick="DeleteNotification({{NotificationId}})"></i></text>
          <text>{{/if}}</text>
  }
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 2011-03-11
    • 2016-05-23
    • 1970-01-01
    相关资源
    最近更新 更多