3

I have two fields date in my form (frm-Pesquisa-Totais-Tempos-UM) and i'm using Jquery serialize to send that informations to controller:

$('.input-daterange').datepicker({
    format: "dd/mm/yyyy",
    todayBtn: "linked",
    language: "pt-BR"
});

$("#btn-Pesquisar").click(function () {
  var form = $('#frm-Pesquisa-Totais-Tempos-UM');

  $.ajax({
    cache: false,
    async: false,
    url: form.attr('action'),
    data: form.serialize(),
    success: function (retorno) {
      if (retorno.success != false) {
        angular.element(document.getElementById('grd-Resultado')).scope().atualizarRegistros(retorno);
      }
    }
  });
});
<div class="col-sm-5 col-md-4 col-lg-3">
    <div class="form-group">
        <label>Período</label>
        <div class="input-daterange input-group" id="datepicker">
        @Html.TextBoxFor(model => model.Parametros.DT_INICIO, new { @id = "txt-dtInicio", @class = "form-control input-sm" })
        <span class="input-group-addon">Até</span>
        @Html.TextBoxFor(model => model.Parametros.DT_FIM, new { @id = "txt-dtFim", @class = "form-control input-sm" })
        </div>
    </div>
</div>

If i use type: "POST" in my ajax call, all dates are putting in the corret format in my controller (dd/mm/yyyy). But when I remove type post and call the controller by "GET", the date arrived in my controller in a different format (mm/dd/yyyy).

I need to use get because when i use post I can't show a loading image during the process.

What it's the best way to correct it?

  • 1
    Your loading image is not displaying due to `async: false` in your `ajax` request because [it halts other ui](http://stackoverflow.com/questions/9755485/stop-browser-locking-during-synchronous-ajax). If you don't need this [async: false](http://stackoverflow.com/questions/1478295/what-does-async-false-do-in-jquery-ajax) then remove it. Else use [fadeIn](http://api.jquery.com/fadein/P) to display your loading image instead of `show` method of jQuery. It will display your loading image regardless of your ajax request type. – mmushtaq Jan 10 '17 at 13:12
  • @mmushtaq what???? – Rajshekar Reddy Jan 10 '17 at 13:13
  • Can you add your controller code too... – Rajshekar Reddy Jan 10 '17 at 13:14
  • this is a known issue. Dates on the querystring are always passed in the "invariantculture" format, which is MM/dd/yyyy. There is a good reason for this, explained well in the accepted answer here: http://stackoverflow.com/questions/528545/mvc-datetime-binding-with-incorrect-date-format. So if you pass a date in dd/mm/yyyy format on the querystring, it will "work" if it happens to parse as mm/dd/yyyy as well (e.g. 10/01/2017), but will be assumed as mm/dd/yyyy by the controller. Otherwise it might even crash. – ADyson Jan 10 '17 at 13:21
  • You can get round it either by always using POST, or by converting your dates to mm/dd/yyyy, or to a format that can't be misinterpreted, like ISO: yyyy/mm/dd before you send it. P.S. Does your controller accept the date as `DateTime` or as `string`? P.P.S. If your `datepicker` is the jquery-ui datepicker then `format: "dd/mm/yyyy"` should be `format: "dd/mm/yy"` - see http://api.jqueryui.com/datepicker/#utility-formatDate – ADyson Jan 10 '17 at 13:23
  • @mmushtaq It's work. Thanks a lot!!!! – Carlos Henrique Biazin Esteves Jan 10 '17 at 13:26

0 Answers0