剃刀DropDownList不禁用 [英] Razor DropDownList Not Disabling
问题描述
我有一个 SelectList
可以填充/正常工作.
我有一个新的要求,以禁用某些用户的列表.因此,我在 ViewModel
, LockSelectList
中添加了一个新属性.此属性已在控制器中设置(并且有效).
但是,呈现列表时,始终将其禁用.
我尝试过
@ Html.DropDownListFor(x => x.Id,Model.AllLocations,新的{disabled = Model.LockLocationList?"true":"false"})@ Html.DropDownListFor(x => x.Id,Model.AllLocations,新的{disabled = Model.LockLocationList?"disabled":"false"})@ Html.DropDownListFor(x => x.Id,Model.AllLocations,新的{disabled = Model.LockLocationList?"disabled":"})
,但无济于事.它们都呈现正确的html,但无论该值禁用了列表如何,似乎Disabled属性的存在.那么,我该如何对代码进行调整才能使其正常工作呢?(最好不要在事件发生后使用jQuery处理启用/禁用)
我建议按照以下方式进行操作...
<代码> @if(Model.LockLocationList){@ Html.DropDownListFor(x => x.Id,Model.AllLocations,新{disabled ="disabled"})}别的{@ Html.DropDownListFor(x => x.Id,Model.AllLocations)}
或者您可以参考以下帖子,根据您的选择有条件地添加它.
有条件的禁用输入 >
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...
@if(Model.LockLocationList)
{
@Html.DropDownListFor(x => x.Id, Model.AllLocations, new {disabled = "disabled" })
}else
{
@Html.DropDownListFor(x => x.Id, Model.AllLocations)
}
OR you can refer following post to add it conditionally as per your choice.
这篇关于剃刀DropDownList不禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!