【问题标题】:Toggle disable button in Razor view ASP.NET MVC 4在 Razor 视图 ASP.NET MVC 4 中切换禁用按钮
【发布时间】:2014-01-04 07:20:23
【问题描述】:

我正在尝试根据 if 语句在 Razor 视图中启用或禁用 html 按钮。无论 if 语句的值如何,这都会禁用按钮。

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

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

【问题讨论】:

  • 顺便说一句,你可以去掉所有这些括号。

标签: html asp.net asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:

简单修复,只需从按钮定义中删除 disabled=""。

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

【讨论】:

【解决方案2】:

如果您想在按钮定义中保留禁用,您可以执行以下操作:

&lt;button @if (@Model.DisableButtonModel) {  @("disabled='1'") }&gt;My Button&lt;/button&gt;

【讨论】:

    猜你喜欢
    • 2013-01-26
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    相关资源
    最近更新 更多