Reputation: 751
Hi folks hi have a very interesting situation.
i have a viewmodel that requires a mediatype from a dropdownlist *by the way the dropdownlist is generated by
@Html.DropDownListFor(m => m.MediaType, Model.MediaTypeList)
and when i fill the form by hand all the values of the form are ok and the functions works the way it should.
but then i implemented a jquery function to set the value of the dropdownlist whenever you upload a file it detects automatically what media type it has.
function setValueToDropDown(media){
$('#MediaType').attr('disabled','');
var value = media;
$('#MediaType').val(value);
}
and when i submit the domain validations tell me that the field is required, even when the dropdownlist is option is selected and when i execute a function in console to verify the value it is different than null.
alert($('#MediaType').val())
so i dont understand why when im assigning the value to the dropdown dynamically mvc doesnt recognize it. i look everywhere and i cant find the problem.
Upvotes: 0
Views: 417
Reputation: 5672
This should be enough to enable and set the value of your <select>
element.
var setValueToDropDown = function(media) {
$('#MediaType').prop('disabled', false).val(media);
};
You should use .prop() to manage properties like disabled
since of version 1.6.
Also make sure you are running the script after DOM ready etc. ;)
Upvotes: 1
Reputation: 150253
I have no idea how the script you run looks like nor what it does. But maybe the script is looking for the value
attribute instead of the value
property
Try using this:
function setValueToDropDown(media){
$('#MediaType').attr('disabled','');
var value = media; // Why do you save media in value...?
$('#MediaType').attr("value", value);
}
Upvotes: 1