What option do I need to set to make a drop down box readonly when using MVCs Html.DropDownList?
I've tried things like....
Html.DropDownList("Types", Model.Types, new { _Enabled = "false" })
...and many different things along this line; alas no joy!
I thought this would be an easy.....and it probably is!
Dropdown (select element) is always read only. User do not edit it. User can select any of the option of its own chice. Unlike other html elements, it dont have a attribute which can make the element readony and prevent users to change its value.
We use <select> and <option> elements to create a drop-down list and use disabled attribute in <select> element to disable the drop-down list element. A disabled drop-down list is un-clickable and unusable.
$('#cf_1268591'). attr("readonly", "readonly");
Try this
Html.DropDownList("Types", Model.Types, new { @disabled = "disabled" })
Regarding the catch 22:
If we use @disabled
, the field is not sent to the action (Mamoud) And if we use @readonly
, the drop down bug still lets you change the value
Workaround: use @disabled
, and add the field hidden after the drop down:
@Html.HiddenFor(model => model.xxxxxxxx)
Then it is truly disabled, and sent to the to the action too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With