asp.net MVC does not recognize dates in dd / mm / yyyy received via GET

2

I'm doing a filter for a paged list. The values of this filter must be sent via ajax with verb GET.

This is the model:

public class FiltroViewModel
{
    public DateTime? CriadosDe { get;set; }
    public DateTime? CriadosAte { get;set; }
}

This is the view: (I do not know if it makes a difference, but I use bootstrap-datepicker's eternicode)

<form class="form-horizontal" data-form-filter>
    <div class="form-group">
        <div class="col-sm-6">
            <div class="input-daterange input-group" id="datepicker">
                @Html.TextBoxFor(m => m.CriadosDe, "{0:d}", new { @class = "input-sm form-control" })
                <span class="input-group-addon">Até</span>
                @Html.TextBoxFor(m => m.CriadosAte, "{0:d}", new { @class = "input-sm form-control" })
            </div>
        </div>
    </div>

Jquery

var formData = $(document).find('[data-form-filter]').serialize();
$.ajax({
    type: "GET",
    url: url,
    dataType: "json",
    data: formData,
    success: function (data) {
      fazAlgo();
    }, 
});

Assuming a Created = 04/10/2016 and a CreatedAtly = 10/28/2016, the serialization looks like this:

&CriadosDe=04%2F10%2F2016&CriadosAte=28%2F10%2F2016

The problem is that the model arrives with these values in the controller:

CriadosDe = 10/04/2016
CriadosAte = null

That is, the server attempted to interpret as mm / dd / yyyy. How to make it always interpret as dd / mm / yyyy?

    
asked by anonymous 04.10.2016 / 17:08

1 answer

1

This is because GET requests are handled by the default model binder and it uses CultureInfo.InvariantCulture, unlike POST requests that use the application's CultureInfo.

In case you have the following alternatives

  • Do not use GET
  • Use GET and change format at link time / URL
  • Change from DateTime to String and then make due treatment in the control (as suggested by @Marconi)
14.01.2017 / 21:55