使用剃须刀的条件Html属性 [英] Conditional Html attribute using razor

查看:169
本文介绍了使用剃须刀的条件Html属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我想显示一个按钮作为启用或禁用状态,这取决于在视图模型上设置的属性。  @if(Model.CanBeDeleted)
{
< button type =buttonclass =btn btn-danger btn-sm>
< span class =glyphicon glyphicon-trash> < /跨度>
删除
< / button>

@if(!Model.CanBeDeleted)
{
< button disabled =disabledtype =buttonclass =btn btn-danger btn-sm >
< span class =glyphicon glyphicon-trash> < /跨度>
删除
< / button>
}

目前在我看来,上面可以看到的代码确实有效。



然而,我正在寻找一种方法,只能在if语句中包装 disabled 属性,而不是对于每种情况都有一个单独的按钮元素。



有关我如何做到这一点的任何建议? 您可以使用 @ Html.Raw 将标记直接注入到元素中

 < button @ Html.Raw(Model.CanBeDeleted?:disabled ='disabled')
type =buttonclass = btn btn-danger btn-sm>
< span class =glyphicon glyphicon-trash> < /跨度>删除
< / button>


I have a situation where I want to display a button as being enabled or disabled depending on a property which has been set on the view model.

@if (Model.CanBeDeleted)
{
    <button type="button" class="btn btn-danger btn-sm">
        <span class="glyphicon glyphicon-trash"> </span>
        Delete
    </button>
}
@if (!Model.CanBeDeleted)
{
    <button disabled="disabled" type="button" class="btn btn-danger btn-sm">
        <span class="glyphicon glyphicon-trash"> </span>
        Delete
    </button>
}

The code currently in my view, which can be seen above, does work.

However, I am looking for a way that I can wrap only the disabled attribute in the if statement rather than having a separate button element for each case.

Any suggestions for how I can do this?

解决方案

You can use @Html.Raw to inject markup directly into elements

<button @Html.Raw(Model.CanBeDeleted?"":"disabled='disabled'") 
        type="button" class="btn btn-danger btn-sm">
  <span class="glyphicon glyphicon-trash"> </span> Delete
</button>

这篇关于使用剃须刀的条件Html属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆