MVC Razor 视图中的把手

Handlebars inside MVC Razor view

我在 ASP.NET MVC 应用程序的剃刀视图中使用手柄。

我有一种情况,我必须在剃须刀的 else 条件中使用 handlebar 的 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 中的条件所需的所有值传递给 handlebars 并将其与模板中的 handlebars 条件一起使用。但我想尽可能使用 Razor。

有什么办法可以做到吗?

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

在 Razor 中使用 <text> 标记可能会帮助您让视图引擎正确处理 handlebars 语法。

试试这个示例代码:

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