Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.DropDownList - Disabled/Readonly

Tags:

asp.net-mvc

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!

like image 943
ETFairfax Avatar asked Oct 28 '09 09:10

ETFairfax


People also ask

How do you do a dropdown in readonly?

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.

How do I GREY out a drop down in HTML?

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.

How set readonly property of dropdown in jquery?

$('#cf_1268591'). attr("readonly", "readonly");


2 Answers

Try this

Html.DropDownList("Types", Model.Types, new { @disabled = "disabled" }) 
like image 79
Tadas Šukys Avatar answered Oct 22 '22 22:10

Tadas Šukys


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.

like image 34
BGStack Avatar answered Oct 22 '22 22:10

BGStack