Toggle disable button in Razor view ASP.NET MVC 4

2019-06-03 21:54发布

I'm trying to toggle between enabling or disabling an html button from a Razor view based on an if statement. This disables the button regardless of the value of the if statement.

@{
    disabled = "";
    if (Model.User.PublicId == ViewBag.SiteUser.PublicId)
    {
        followClass += " you";
        followText = "You";
        disabled = "disabled";
    }
}

<button class="@followClass" disabled="@(disabled)">@(followText)</button>

2条回答
Explosion°爆炸
2楼-- · 2019-06-03 22:17

If you want to keep the disabled in the button definition you can do the following:

<button @if (@Model.DisableButtonModel) {  @("disabled='1'") }>My Button</button>

查看更多
家丑人穷心不美
3楼-- · 2019-06-03 22:31

Simple fix, just remove disabled="" from the button definition.

<button class="@followClass" @disabled >@followText</button>
查看更多
登录 后发表回答