0

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.

4
  • Is value the proper value of the option you want to select? Or it rapresent the test in that option? Commented May 21, 2012 at 8:58
  • in that case the value is the one i want to select, i just put it like that to put a breakpoint there and see the value in firebug i realize now that it looks a little dummy since i can see the media val from the parameter Commented May 21, 2012 at 9:13
  • thank you in the end decided to create a hidden input field and map it to that in my view model, i already spend to many time in this Commented May 21, 2012 at 15:08
  • Regarding to your's accepted answer. Can you please explain how did it help you? it looks like it does exactly what your code does. Commented May 21, 2012 at 15:55

2 Answers 2

1

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. ;)

Sign up to request clarification or add additional context in comments.

Comments

1

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);   
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.