剃刀DropDownList不禁用 [英] Razor DropDownList Not Disabling

查看:53
本文介绍了剃刀DropDownList不禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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.

Conditional disable input

这篇关于剃刀DropDownList不禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆