IAmGroot
IAmGroot

Reputation: 13855

Jquery ajax posting date

I'm baffled why my controller is not recieving the startDate parameter.. from the jquery datepicker

My thoughts are it has something to do with the format.

In this scenario I cant force it using the model for form posting.

The parameter is coming into the action as null.

    $.ajax({
        url: '@Url.Action("myAction")',
        type: 'GET',
        cache: false,
        data: { startDate: $('#startDate').val(), endDate: $('#endDate').val() },
        success: function (result) {
            $('#updateDiv').html(result);
        }
    });

Controller

    public ActionResult myAction(DateTime? startDate, DateTime? endDate)

Ive also tried;

    startDate: $.datepicker.formatDate('dd/mm/yy', new Date())

Upvotes: 0

Views: 8874

Answers (1)

musefan
musefan

Reputation: 48415

Yes it will likely be a format problem - I forget the exact causes.

I would suggest changing the controller action parameters to string types instead, then you can manual parse them in the correct format, this is the way I always do it and never have any problems since moving to this method.

Something like this:

public ActionResult myAction(string startDate, string endDate)
{
    DateTime? startDT = null;
    if(!string.IsNullOrWhitespace(startDate))
    {
        startDT = DateTime.ParseExact(startDate, "dd/MM/yyyy", null);
    }
}

might be better to use TryParseExact instead, depends on how you want to handle bad data really

Upvotes: 4

Related Questions