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

Asked

Viewed 100 times

2

I am making a filter for a paged list. The values of this filter should 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 don’t know if it makes a difference, but I always use bootstrap-datepicker)

<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 Creator = 10/04/2016 and a Creator = 10/28/2016, the serialization is 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 tried to interpret as mm/dd/yyyy. How to make it always interpret as dd/mm/yyyy?

  • 1

    Ever tried to change the DateTime for String?

  • Putz, great idea, I hadn’t thought about it yet! Leave as string already solves everything, even. Thank you. Anyway, let’s see if an answer comes up to use Datetime.

  • I asked a question, see: http://answall.com/questions/156655/varchar-ou-datetime?noredirect=1#comment323008_156655

1 answer

1


This is because GET requests are handled by the standard Binder model 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 the format when placing on the link/URL
  • Switch from Datetime to String and then do the proper treatment in control (as suggested by @Marconi)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.