Html 在 asp.net mvc 中获取禁用下拉列表的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11351554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get value of disabled drop down in asp.net mvc
提问by Brij
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.
我有一个 ASP.NET MVC 应用程序。我的页面中有多个下拉列表(HTML SELECT),我必须禁用它们,因为用户会一一选择它们。当用户将它发回控制器时,我得到 null 作为函数(操作方法)参数。我搜索并发现 HTML 不会在表单数据中发送禁用字段的值。用 readonly 替换 disabled 属性不起作用,因为它会使下拉列表工作。
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.
随着用户的继续,我正在使用 javascript 动态生成下拉列表。所以没有一个下拉菜单,而是用户想要的数量。
Can someone please tell me how should I get the values ?
有人可以告诉我应该如何获得这些值吗?
回答by Darin Dimitrov
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:
一种可能性是制作下拉列表disabled="disabled"
并包含一个具有相同名称和值的隐藏字段,这将允许将此值发送到服务器:
@Html.DropDownListFor(x => x.FooId, Model.Foos, new { disabled = "disabled" })
@Html.HiddenFor(x => x.FooId)
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.
如果您必须使用 javascript 动态禁用下拉列表,则只需在禁用下拉列表后将其当前选定的值分配给隐藏字段。
回答by Felipe Oriani
This is the default behavior of disabled controls. I suggest you to add a hidden field and set the value of your DropDownList in this hidden field and work with this.
这是禁用控件的默认行为。我建议您添加一个隐藏字段并在此隐藏字段中设置 DropDownList 的值并使用它。
Something like:
就像是:
//just to create a interface for the user
@Html.DropDownList("categoryDump", (SeectList)ViewBag.Categories, new { disabled = "disabled" });
// it will be send to the post action
@Html.HiddenFor(x => x.CategoryID)
回答by Peter
You could also create your own DropDownListFor overload which accepts a bool disabled
parameter and does the heavy lifting for you so your view isn't cluttered with if disablethisfield then ...
.
您还可以创建自己的 DropDownListFor 重载,它接受一个bool disabled
参数并为您完成繁重的工作,这样您的视图就不会被if disablethisfield then ...
.
Something among these lines could do:
这些行中的一些事情可以做:
public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, bool disabled)
{
if (disabled)
return MvcHtmlString.Create(htmlHelper.HiddenFor(expression).ToString() + htmlHelper.DropDownListFor(expression, selectList, new { disabled="disabled" }).ToString());
else
return htmlHelper.DropDownListFor(expression, selectList);
}
There are 6 overloads for DropDownListFor alone so it's a lot of monkeycoding but it pays off in the end imho.
仅 DropDownListFor 就有 6 个重载,因此需要大量的猴子编码,但最终还是有回报的。
回答by raza
before submit call $('#FooId').removeAttr('disabled')
在提交之前调用 $('#FooId').removeAttr('disabled')