Making a drop down list readonly but still submitting its value
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.
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.
Html.DropDownList("Types", Model.Types, new { @disabled = "disabled" })
Does anyone know how to make a drop down list rea开发者_Go百科d-only or disabled with ASP.NET MVC while also allowing it to submit with a form post?
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.
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.
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.
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());
});
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.
Just alter the stylesheet of that item.
visibility:"visible"
visibility:"hidden"
For more information, see http://support.microsoft.com/kb/199243.
You will still get the postback but you also won't see it anymore.
i think the best solution is to post it via
$.post(url,
{
AID: val1,
CID: val2
},
function (data) {
// act on model
}
);
精彩评论