This is how I've accomplished this but I'd love to know if there are better ways.
Using the same options in the question, I then also made the HTML text box readonly
by doing this in my HTML:
@Html.EditorFor(model => model.Date, new { htmlAttributes = new {
@class = "form-control datepicker", @readonly = "true",
}})
This seems to work as JavaScript can still change the value of the field. However, it does make the background of the text box gray (i.e., disabled) and it changes the mouse pointer to the crossed circle icon (not-allowed
) which is not desired. To get around that I did this added an inline style:
@Html.EditorFor(model => model.Date, new { htmlAttributes = new {
@class = "form-control datepicker", @readonly = "true",
style = "cursor: default; background-color: #fff"
}})
I tried to override the cursor
and background-color
in CSS (using !important
) but that didn't seem to work in Internet Explorer.
This isn't pretty but it works for me. Still need to see how I could do this in a more robust way (e.g., no hard coding the background color, not having to specify this on all of my date fields, etc.). If anyone knows a better way, I'd very much appreciate it.
new { htmlAttributes = new { ... } }
? I typically use onlynew { ... }
. Is this necessarily bad? – Bolshevist