0

I am using Ajax request to send the value of date to my API Controller here is my ajax call

 $("#txtpdate").on("changeDate", function (e) {
        var Date =$('#txtpdate').val();
        console.log(Date);
        $.ajax({
            type: 'POST',
            contentType: false,
            url: '/api/ServiceProvider/GetUpdatedPrice',
            data:{date: Date  },
            cache: false
        }).done(function (response) {

        });
});

And here is my api controller

[HttpPost]
[Route("GetUpdatedPrice")]
public  ServiceProviderDocuments GetUpdatedPrice(DateTime date)
{
    return  ServiceProviderDocumentsGateway.GetUpdatedPriceofBike(date);
}

I am getting the desired value in console.log(Date) but when I reached to the controller it is showing null value please take a look on the below image enter image description here

4
  • try this one - data: JSON.stringify({date: Date }) Commented Jun 5, 2017 at 9:55
  • check this! Commented Jun 5, 2017 at 9:56
  • What does console.log(Date); log to your console? Commented Jun 5, 2017 at 10:00
  • this is showing in console 06/20/2017 Commented Jun 5, 2017 at 10:01

5 Answers 5

1

Just solved by doing this

  var dt=$('#txtpdate').val();
    ko.toJSON({ date: new Date(dt)})

then you will get date in action.

Sign up to request clarification or add additional context in comments.

Comments

0

hi try to parse request into json and your date field into date like to: new Date

 var promise=jQuery.ajax({
        url: url,
        type: 'POST',
        data: JSON.stringify(data),
        contentType: 'application/json; charset=utf-8'
    }).promise();

Comments

0

You should declare the date parameter as DateTime, like:

[HttpPost]
[Route("GetUpdatedPrice/{pDate:datetime}")]
public  ServiceProviderDocuments GetUpdatedPrice(DateTime pDate)
{
    return ServiceProviderDocumentsGateway.GetUpdatedPriceofBike(pDate);

}

Comments

0

Do the following:

  1. Change the type of the parameter to string:

    [HttpPost]
    [Route("GetUpdatedPrice")]
    public  ServiceProviderDocuments GetUpdatedPrice(string date)
    {
        //Convert date to Datetime here
    }
    
  2. Change the contentType to:

    contentType: 'application/json; charset=utf-8',
    
  3. Stringify the date:

    data: JSON.stringify({ date: Date }),
    

9 Comments

still getting null in controller
@Gaurav_0093 This should works. Are you sure that you did all the steps? Can you show your updated code?
yes sir here is my code- $.ajax({ type: 'POST', contentType: 'application/json;', url: '/api/ServiceProvider/GetUpdatedPrice', data: JSON.stringify({ date: Date }), cache: false }).done(function (response) { in controller [HttpPost] [Route("GetUpdatedPrice")] public ServiceProviderDocuments GetUpdatedPrice(string date) { return ServiceProviderDocumentsGateway.GetUpdatedPriceofBike(date); }
@Gaurav_0093 Please show the updated code in your question. Edit your question and show your new code.
@S.Akbari-Sir this one is the updated code in my question
|
0

In my case, the date was a property of a object and the "set" was only "internal", so I remove it and now it's working.

After:

public DateTime DataInicialFiltro { get; internal set; }

Before:

public DateTime DataInicialFiltro { get; set; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.