I have a SelectList
that populates / works ok.
I have a new Requirement to disable the list for certain users. So I added in a new property to the ViewModel
, LockSelectList
. this property is set in the controller (and works).
However, when the list is rendered, it is always disabled.
I have tried
@Html.DropDownListFor(x => x.Id, Model.AllLocations, new {disabled = Model.LockLocationList? "true" : "false" })
@Html.DropDownListFor(x => x.Id, Model.AllLocations, new {disabled = Model.LockLocationList? "disabled" : "false" })
@Html.DropDownListFor(x => x.Id, Model.AllLocations, new {disabled = Model.LockLocationList? "disabled" : "" })
but none work. they all render correct html, but it seems that the presence of the disabled attribute, no matter the value disables the list. So what do I tweak to the code make this work? (Preferably without using jQuery to handle the enable / disable after the event)
I would recommend to do it following way...
OR you can refer following post to add it conditionally as per your choice.
Conditional disable input
Or you can do this as below if condition is true make an object which contains disable attribute else blank object. I hope it'll help you
Whenever the
disabled
attribute is present on the dropdown list, it is disabled not matter the value. So when it's not supposed to be disabled, you need to not set it at all.See K D's answer for a code example