Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 2.64 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Suggestions please for finishing multi select list date in MVC Razor and Knockout
  2. @{
  3.     var yearOptions = Enumerable.Range(1930, 2010-1930+1);
  4.     var monthOptions = System.Globalization.DateTimeFormatInfo.CurrentInfo
  5.         .MonthNames
  6.         .TakeWhile(x => x != String.Empty)
  7.         .Select((x,i) => new  
  8.         {  
  9.           Value = i,  
  10.           Text = x
  11.         });
  12. }
  13.  
  14.  
  15. <div id="the_model">
  16.     <form>
  17.         <div class="editor-row">
  18.             <label>Date of Birth</label>
  19.             @Html.HiddenFor(m => m.BirthDate, new { data_bind = "value: birthDate" })
  20.             <select data-bind="options: yearOptions, value: birthDateYear, optionsCaption: 'year'"></select>&nbsp;
  21.             <select data-bind="enable: birthDateYear, options: monthOptions, optionsValue: 'Value', optionsText: 'Text', value: birthDateMonth, optionsCaption: 'month'"></select>&nbsp;
  22.             <select data-bind="enable: birthDateMonth, options: dayOptions, value: birthDateDay, optionsCaption: 'day'"></select>&nbsp;
  23.         </div>
  24.     </form>
  25. </div>
  26.  
  27. <script type="text/javascript">
  28.     function daysInMonth(month, year) {
  29.         return new Date(year, month, 0).getDate();
  30.     }
  31.  
  32.     var yearOptions = @Html.Raw(Json.Encode(yearOptions));
  33.     var monthOptions = @Html.Raw(Json.Encode(monthOptions));
  34.  
  35.     var TheModel = function () {
  36.         var self = this;
  37.  
  38.         this.birthDate = ko.observable('@Model.BirthDate');
  39.         this.birthDateYear = ko.observable();
  40.         this.birthDateMonth = ko.observable();
  41.         this.birthDateDay = ko.observable();
  42.  
  43.         this.dayOptions = ko.observableArray([]);
  44.  
  45.         this.submit = function () {
  46.             // validate and post
  47.         };
  48.  
  49.             this.birthDateYear.subscribe(function (val) {
  50.     console.log('birthDateYear: ' + val);
  51.     if(parseInt(val) > 0) {
  52.         self.fillDayOptions();
  53.     }
  54. });
  55.  
  56. this.birthDateMonth.subscribe(function (val) {
  57.     console.log('birthDateMonth: ' + val);
  58.     if(parseInt(val) > 0) {
  59.         self.fillDayOptions();
  60.     }
  61. });
  62.  
  63. this.birthDateDay.subscribe(function (val) {
  64.     console.log('birthDateDay: ' + val);
  65.     if(parseInt(val) > 0){
  66.         self.birthDate(getBirthDate(self.birthDateYear(), self.birthDateMonth(), val));
  67.     }
  68. });
  69.  
  70. this.fillDayOptions = function() {
  71.     //self.birthDateDay({});
  72.  
  73.     var month = self.birthDateMonth();
  74.     var year = self.birthDateYear();
  75.  
  76.     if(month && year) {
  77.         self.dayOptions([]);
  78.         for(var  i = 0; i < daysInMonth(month, year); i++) {
  79.             self.dayOptions.push(i + 1);
  80.         }
  81.     }
  82.  
  83.     //self.birthDateDay(null);
  84. };
  85.  
  86.     var viewModel = new TheModel();
  87.     ko.applyBindings(viewModel, document.getElementById("the_model"));
  88. </script>