I am working in MVC and I have to bind dropdown values to an array in javascript. How do I do that?
dropdown list-
Html.DropDownListFor(m => m.MyActivityRequest.ToMonth, Model.MonthNames, new { @id = "monthToSelect" })
Javascript function:
$(document).ready(function(){
$("#yearToSelect").change(function(){
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var date = new Date();
var monthVar = date.getMonth();
var yearVar = date.getFullYear();
if ($(this).val() < yearVar ){
$("#monthToSelect").find('option').remove();
for( i = 0; i < 12; i++ ){
$("#monthToSelect").append($("<option>",{
value: months[i],
text: months[i]
}));
};
}
you can see that the the array- 'months' is hard coded as of now. I want to dynamically bind the dropdown values to this array.