2013-12-16 71 views
1

我想根據if語句在啓用或禁用Razor視圖中的html按鈕之間切換。無論if語句的值如何,都會禁用該按鈕。在Razor視圖中切換禁用按鈕ASP.NET MVC 4

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

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

順便說一句,你可以擺脫所有這些括號的。 – SLaks

回答

1

簡單的修復,只需從按鈕定義中刪除disabled =「」。

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

Disabled是一個布爾屬性 - 請參閱https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input - 屬性的存在表示該控件已禁用 - 「禁用」本身是與disabled =「disabled」一樣。正如您發現刪除該屬性將啓用該控件。 – pwdst

0

如果你想保持在禁用按鈕定義,你可以做到以下幾點:

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