2014-09-01 35 views
0

這是我的嘗試:有條件啓用/禁用asp.net的MVC文本框

@Html.TextBoxFor(model => model.EntryDate, new { style="width:90px;",((_new || _modify) ? @disabled="disabled":"") }) @Html.ValidationMessageFor(model => model.EntryDate) 
+0

廣東話你只需要使用@if(comeCondition){@ Html.TextBo xFor(model => model.EntryDate)} else {@ Html.TextBoxFor(model => model.EntryDate,new {@disabled =「disabled」)} – 2014-09-01 12:03:33

+0

嗯,我希望有一個簡單而優雅的方式去它 – POIR 2014-09-01 12:04:44

+0

好的,會發布回答 – 2014-09-01 12:32:07

回答

0

我不知道,但嘗試這一個:

@Html.TextBoxFor(model => model.EntryDate, (_new || _modify) ? new{ @Style="width:90px;", @Disabled="disabled"} : new{ @Style="width:90px;"}) 
+0

你確定這些匿名類型是否兼容?你測試過了嗎? – Charleh 2014-09-01 12:51:18

1

下面的工作,因爲只有一個html屬性

@Html.TextBoxFor(m => m.EntryDate, (_new || _modify) 
    ? new { @disabled = "disabled" } 
    : null) 

但是,一旦添加了style屬性,這將產生一個錯誤,因爲沒有隱式轉換吐溫和new { @Style="width:90px;" }所以你需要做到這一點在if

@if(_new || _modify) { 
    @Html.TextBoxFor(m => m.EntryDate, new { @disabled = "disabled", style="width:90px;")} 
} else { 
    @Html.TextBoxFor(m => m.EntryDate, new { style="width:90px;" }) 
} 

編輯

鑄造HTML字典object似乎工作,但我還沒有完全測試這個(我不知道它的「一個簡單而優雅的方式來給它」

@Html.TextBoxFor(m => m.EntryDate, (_new || _modify) 
    ? (object)new { @disabled = "disabled", style="width:90px;" } 
    : (object)new { style="width:90px;" }) 
相關問題