Я использую рули внутри бритвенного представления в приложении ASP.NET MVC.
У меня есть ситуация, когда я должен использовать условие if руля внутри условия else бритвы, как показано ниже.
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 не компилируется. Знак "#" в строке {{#if UserCanSetClosed}} рассматривается как директива препроцессора. Если быть точным, код внутри части else обрабатывается как код C#, а не как дескрипторы.
Использование тега <text>
в Razor может помочь вам правильно обработать синтаксис руля в View Engine.
Попробуйте этот пример кода:
<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>
Что не так с тем, что ты делаешь сейчас?