2016-02-24 33 views
0

我試過幾個答案張貼在stackoverflow。但是,以下似乎不工作:剃刀視圖 - C# - 禁用只讀文本框的

@Html.TextArea("Comments", Model.Comments, Model.ReadOnly ? new { @disabled = "disabled"} : null) 

我也試過:

@Html.TextArea("Comments", Model.Comments, Model.ReadOnly ? new { disabled = "disabled"} : null) 

任何想法,我究竟做錯了什麼?

+0

您的三元操作你看了我的問題嗎?我已經試過 – user5975648

+0

你沒有具體說明你已經看過的答案,我不知道你是否曾經看過。 –

+0

對不起,這是我的錯誤。感謝您指出 – user5975648

回答

0

我會使用自定義變量來設置它。我認爲這個條件在HtmlHelper的論點中是不可接受的。

@{ 
    var htmlAttributes = Model.ReadOnly ? new { disabled = "disabled" } : null; 
} 

@Html.TextArea("Comments", Model.Comments, htmlAttributes) 
+0

我要去嘗試一下!任何想法,如果我可以添加類似的參數,真實和錯誤的條件?沒有提到 – user5975648

+0

只要結果是一個有效的屬性集合,你應該能夠以這種方式構建它。 –

0

我不認爲MVC在這裏喜歡null。你需要爲第三個參數(其預計object)什麼是默認是空的匿名實例,而不是null

Model.ReadOnly ? (object)new { disabled = "disabled" } : (object)new { } 
0

嘗試包裹在括號

@Html.TextArea("Comments", Model.Comments, (Model.ReadOnly ? new { @disabled = "disabled"} : null))