title: MVC 5 - jQuery DatePicker changed date - postback to controller
body: <p>I'm trying find a way to make an Ajax form postback when either the datepicker selected date is changed, or use a hidden textbox and use the onchanged method, but can't find a way to do it (change the date in a datepicker and then hit a submit button and it gets back the controller...)</p>
  
  <p>View</p>
  
  <pre><code>&lt;div class="panel-body"&gt;
      @Using (Ajax.BeginForm(Nothing, "Calendar/DateChanged", Nothing, New AjaxOptions With {.HttpMethod = "POST"}, New With {.id = "SelectDateForm"}))
          @&lt;div&gt;
      &lt;div class="form-group"&gt;
          &lt;div id="datepicker"&gt;&lt;/div&gt;
  
          @Html.TextBoxFor(Function(x) x.Current_Date, "{0:MM/dd/yyyy}", New With {.id = "inputMaintDate", .class = "form-control", .type = "date", .onchange = "SelectDateForm.submit();"})
      &lt;/div&gt;
               &lt;div class="form-group"&gt;
                   &lt;button id="LoginButton" class="btn btn-primary" type="submit"&gt;&lt;span class="glyphicon glyphicon-save"&gt;&lt;/span&gt; Save&lt;/button&gt;
  
               &lt;/div&gt;
  
  
      &lt;/div&gt;
  
      End Using
  
  &lt;/div&gt;
  </code></pre>
  
  <p>Scripts</p>
  
  <pre><code> $(document).ready(function () {
      $('#inputMaintDate').change(function () {
          $.ajax({
              type: "POST",
              data: $('SelectDateForm').serialize(),
              success: function (response) {
              }
          });
      });
  });
  
  
   $(document).ready(function () {
  
      $('#datepicker').datepicker({
          altField: '#inputMaintDate'
          //inline: true
                  });
          });
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>  &lt;HttpPost()&gt;
      Function DateChanged(vModal As CalendarModels)
          Try
              Dim vDate As Date = vModal.Current_Date
              Return ModalValidation(vDate.ToString)
          Catch ex As Exception
              EmailError(ex)
              Return SystemError()
          End Try
      End Function
  </code></pre>
  
  <p>============ EDIT =====================</p>
  
  <p>By adding an onchange event to the datepicker</p>
  
  <pre><code>&lt;div id="datepicker" onchange="SelectDateForm.submit();"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>It now posts back to the controller DateChanged function, but instead of sending back a JavaScriptResult it asks the user if they want to run or save DateChangedxxxxxx.js. Had this issue when we moved to .NET 4.6 and adding jquery.unobtrusive-ajax.js fixed it</p>
  
  <p>Big sigh!</p>
  
  <p>What is the main difference between posting back from a button or using a control onchange event?</p>