1

I have a json object like this :

var data = {}; 
data = 
 { "First" : _this.getFirstElement.val(), 
    "Second": _this.getSecondElement.val(),
    "DateProperty" : _this.getDatePropertyElement.val()
 }; 

This is passed to the controller method using jquery post :

$.post(url,data,function(){somefunction}); 

My controller method is like this :

[HttpPost]
public void ControllerMethod(TestObject data)
{
    // data.DateProperty is coming as null
   // logic 
}

What did i miss?

The FormData passed when looked in the network section of browser specifies data as :

      FormData : 
      First : <some data>
      Second : <some data>
      DateProperty : some long date string 

Later i changed json data to send date as JSON.stringify()

Now in the formData , value is coming as

    First : <some data>
    Second : <some data>
    DateProperty : "05/06/16"

But still at Controller method, dateProperty is null ?

SunilA
  • 513
  • 8
  • 19
  • What you've described should work fine. Are you sure that the property name for `DateProperty` match whats defined in the C# model? – Rory McCrossan May 19 '16 at 18:36
  • @RoryMcCrossan - yes. The property is same at C# side. – SunilA May 19 '16 at 18:39
  • I had a weird observation - when i hardcode the value of post data from the browser and set the dateProperty as "09/07/10". It gave correct date value at controller. – SunilA May 19 '16 at 18:41
  • using dateFormat in jquery for the date value worked for me. Got Idea from : http://blog.stevenlevithan.com/archives/date-time-format – SunilA May 20 '16 at 06:54

1 Answers1

0

Formatting the date value from javascript using dateFormat resolved the issue.

This is how I formatted the date value :

var data = {}; 
data = {
    "First" : _this.getFirstElement.val(), 
    "Second": _this.getSecondElement.val(),
    "DateProperty" : dateFormat(_this.getDatePropertyElement.val(), "mm/dd/yyyy HH:MM:ss"),
};

With this date value is received at the controller method.

References : http://blog.stevenlevithan.com/archives/date-time-format

Danscho
  • 466
  • 5
  • 20
SunilA
  • 513
  • 8
  • 19