Thursday, March 7, 2013

How to convert Viewbag Value as Array Using Javascript in MVC

function GetMonth(monthStart) {
   if (monthStart == 1) {
       $('#ddlExpirationMonth').empty();
       $('#ddlExpirationMonth').append('<option value="0">MM</option>');
     
       var array = @Html.Raw(Json.Encode(@ViewBag.CreditCardMonth));
      
       for(var i = 0; i < array.length; i++) {
       $('#ddlExpirationMonth').append('<option value="' + array[i].Id
+  '">' + array[i].Name + '</option>');
  }
     }
      else {
     for (var j = 1; j < monthStart; j++) {
       if (j < 10)
       j = "0" + j;
       $("#ddlExpirationMonth option[value='" + j + "']").remove();
}
}
}

No comments:

Post a Comment