asp.net-mvc 将下拉列表设为只读但仍提交其值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2858511/
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
Making a drop down list readonly but still submitting its value
提问by YeahStu
I am using the Html.DropDownList helper in ASP.NET MVC and I would like to make it read-only. Unfortunately, I also need it to submit its value on a form post.
我在 ASP.NET MVC 中使用 Html.DropDownList 帮助程序,我想将其设置为只读。不幸的是,我还需要它在表单帖子上提交其值。
I have found (through a similar question on SO) that using the below format will make the drop down read-only but it will not provide access to the control's value within the controller.
我发现(通过SO 上的一个类似问题)使用以下格式将使下拉列表只读,但它不会提供对控制器中控件值的访问。
Html.DropDownList("Types", Model.Types, new { @disabled = "disabled" })
Does anyone know how to make a drop down list read-only or disabled with ASP.NET MVC while also allowing it to submit with a form post?
有谁知道如何使用 ASP.NET MVC 将下拉列表设为只读或禁用,同时还允许它通过表单发布提交?
回答by Prescott
It's intended behavior on the web browser side - a disabled control will not post it's data to the server when a form is submitted.
这是 Web 浏览器端的预期行为 - 提交表单时,禁用的控件不会将其数据发布到服务器。
You can fake it by putting a hidden field on the page with the value in it - just be sure to validate the data. Or use javascript to enable the field before the submit action happens.
您可以通过在页面上放置一个包含值的隐藏字段来伪造它 - 只需确保验证数据即可。或者在提交操作发生之前使用 javascript 启用该字段。
If you're disabling a field, but still showing it on the page with some value, then there must be a way for you to know that value without having it send back from the browser to the server.
如果您禁用了某个字段,但仍将其显示在页面上并带有某个值,则必须有一种方法可以让您知道该值,而无需将其从浏览器发送回服务器。
回答by Jan Willem B
You can enable it on submit. Downside: it looks weird.
您可以在提交时启用它。缺点:看起来很奇怪。
$(form).submit(function() {
$('#Types').removeAttr('disabled');
});
or copy the value into a hidden field on submit.
或在提交时将该值复制到隐藏字段中。
$(form).submit(function() {
$('#HiddenField').val($('#Types').val());
});
回答by Burg
- Make it a textbox instead.
Add a change event with JavaScript:
$('#dropdown').change(function(e) { e.preventDefault(); });
Have a dropdown list with only 1 item in it.
- Have a hidden item with the actual value of the dropdown in it, so that gets submitted even if the disabled dropdown doesn't.
- 将其改为文本框。
使用 JavaScript 添加更改事件:
$('#dropdown').change(function(e) { e.preventDefault(); });
有一个只有 1 个项目的下拉列表。
- 有一个隐藏项目,其中包含下拉列表的实际值,这样即使禁用的下拉列表没有提交,也会提交。
回答by Pinch
Just alter the stylesheet of that item.
只需更改该项目的样式表。
visibility:"visible"
visibility:"hidden"
For more information, see http://support.microsoft.com/kb/199243.
有关详细信息,请参阅http://support.microsoft.com/kb/199243。
You will still get the postback but you also won't see it anymore.
您仍然会收到回发,但您也不会再看到它。
回答by Stefan Michev
i think the best solution is to post it via
我认为最好的解决方案是通过发布
$.post(url,
{
AID: val1,
CID: val2
},
function (data) {
// act on model
}
);