mvc html.dropdownlist onchange,Html.DropDownList - Disabled/Readonly

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

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!

回答1:

Try this Html.DropDownList("Types", Model.Types, new { @disabled = "disabled" })

回答2:

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.

回答3:

回答4:

Or you can try something like this: Html.DropDownList("Types", Model.Types, new { @readonly = "true" })

回答5:

I just do this and call it a day Model.Id > -1 ? Html.EnumDropDownListFor(m => m.Property, new { disabled = "disabled" }) : Html.EnumDropDownListFor(m => m.Property)

回答6:

I had to disable the dropdownlist and hide the primary ID @Html.LabelFor(model => model.OBJ_ID, "Objs", htmlAttributes: new { @class = "control-label col-md-2" })

@Html.DropDownList("OBJ_ID", null, htmlAttributes: new { @class = "form-control", @disabled = "disabled"}) @Html.HiddenFor(m => m.OBJ_ID) @Html.ValidationMessageFor(model => model.OBJ_ID, "", new { @class = "text-danger" })

回答7:

A tip that may be obvious to some but not others..

If you're using the HTML Helper based on DropDownListFor then your ID will be duplicated in the HiddenFor input. Therefore, you'll have duplicate IDs which is invalid in HTML and if you're using javascript to populate the HiddenFor and DropDownList then you'll have a problem.

The solution is to manually set the ID property in the htmlattributes array... @Html.HiddenFor(model => model.Entity) @Html.EnumDropDownListFor( model => model.Entity, new { @class = "form-control sharp", onchange = "", id =` "EntityDD", disabled = "disabled" } )

回答8:

I've create this answer after referring above all comments & answers. This will resolve the dropdown population error even it get disabled.

Step 01

Html.DropDownList("Types", Model.Types, new {@readonly="readonly"})

Step 02 This is css pointerevent remove code.

Then you can have expected results

Tested & Proven

回答9:

Put this in style select[readonly] option, select[readonly] optgroup { display: none; }

回答10:

@Html.DropDownList("Types", Model.Types, new { @disabled = "" })

Works

回答11:

Html.DropDownList("Types", Model.Types, new { @disabled = "disabled" }) @Html.Hidden(Model.Types) and for save and recover the data, use a hidden control

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值