在 asp.net mvc 中获取禁用下拉菜单的值 [英] Get value of disabled drop down in asp.net mvc

查看:24
本文介绍了在 asp.net mvc 中获取禁用下拉菜单的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ASP.NET MVC 应用程序.我的页面中有多个下拉列表(HTML SELECT),我必须禁用它们,因为用户继续选择它们.当用户将它发回控制器时,我得到 null 作为函数(操作方法)参数.我搜索并发现 HTML 不会在表单数据中发送禁用字段的值.用 readonly 替换 disabled 属性不起作用,因为它会使下拉列表工作.

I have an ASP.NET MVC application. I am having multiple drop-down list in my page (HTML SELECT), I have to disable them, as user goes on selected them one by one. When the user posts it back to the controller, I am getting null as the function (action method) paramters. I searched and found that HTML does not send value of disabled fields in the form data. Replacing disabled attribute with readonly would not work as it would render drop-down working.

随着用户的进行,我正在使用 javascript 动态生成下拉列表.所以没有一个下拉菜单,而是根据用户的需要.

I am generating the dropdowns dynamically using javascript as user goes on. So there isn't a single dropdown, but as many as user wants.

谁能告诉我我应该如何获得这些值?

Can someone please tell me how should I get the values ?

推荐答案

一种可能性是使下拉列表 disabled="disabled" 并包含一个具有相同名称和值的隐藏字段,它将允许将此值发送到服务器:

One possibility is to make the dropdown list disabled="disabled" and include a hidden field with the same name and value which will allow to send this value to the server:

@Html.DropDownListFor(x => x.FooId, Model.Foos, new { disabled = "disabled" })
@Html.HiddenFor(x => x.FooId)

如果您必须使用 javascript 动态禁用下拉列表,则只需在禁用下拉列表后将其当前选定的值分配给隐藏字段即可.

If you have to disabled the dropdown dynamically with javascript then simply assign the currently selected value of the dropdown to the hidden field just after disabling it.

这篇关于在 asp.net mvc 中获取禁用下拉菜单的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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