這就是我已經完成的,但我很想知道是否有更好的方法。
使用中的問題相同的選項,然後我也通過我的HTML這樣做的HTML文本框readonly
:
@Html.EditorFor(model => model.Date, new { htmlAttributes = new {
@class = "form-control datepicker", @readonly = "true",
}})
這似乎是工作,如JavaScript仍然可以改變字段的值。但是,它確實使文本框的背景變灰(即禁用),並將鼠標指針更改爲不需要的交叉圓圖標(not-allowed
)。要解決這個問題我這樣做增加了一個內嵌樣式:
@Html.EditorFor(model => model.Date, new { htmlAttributes = new {
@class = "form-control datepicker", @readonly = "true",
style = "cursor: default; background-color: #fff"
}})
我試圖覆蓋CSS的cursor
和background-color
(使用!important
),但似乎並沒有在Internet Explorer中工作。
這不是很漂亮,但它適用於我。還需要看看我如何以更強大的方式來完成此任務(例如,不需要對背景顏色進行硬編碼,也不必在所有日期字段中指定此類等)。如果有人知道更好的方法,我會非常感激。
爲什麼使用'new {htmlAttributes = new {...}}'?我通常只使用'new {...}'。這一定是不好的? –