Home > DeveloperSection > Forums > Send date from my front end (angular) to c# .net datetime (webapi)
Lillian Martin

Total Post:27

Points:189
Posted on    November-21-2014 12:32 AM

 ASP.Net C#  .NET  JSON  Angular JS 
Ratings:


 1 Reply(s)
 1760  View(s)
Rate this:

On my front-end I have a HTML input box where the user can enter a date. This adds data to my AngularJS modal and I then try to send it to the server as a JSON object when the user clicks save.

On the server I have used this which I think modifies the way dates are expected. All I know is that it allows AngularJS to nicely read the dates coming from the back-end.

     config.SuppressDefaultHostAuthentication();

        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

        config.Formatters.Remove(config.Formatters.XmlFormatter);

        var json = config.Formatters.JsonFormatter;

        json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

        json.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

        json.SerializerSettings.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-ddTHH:mmZ" });

Does anyone have any ideas how I can take a date entered into the input box in a format such as YYYY-MM-DD, modify in some way and then pass this to the server and have the server read it as a datetime?

The problem I have is that I am not sure how I can modify the YYYY-MM-DD date. When I try entering in a date like "2011-12-31" it does not get accepted by the server and the column that it should be entered into shows a null value for the date.



Pravesh Singh

Total Post:411

Points:2881
Posted on    November-21-2014 10:25 PM

Have you ever tried to create a subclass of IsoDateTimeConverter? Try this:

public class MyJsonDateTimeConverter : IsoDateTimeConverter

{

    public MyJsonDateTimeConverter()

    {

        base.DateTimeFormat = "yyyy-MM-dd";

    }

}

Then use it in place of the last line:

json.SerializerSettings.Converters.Add(new MyJsonDateTimeConverter());

Hope this helps :)


Don't want to miss updates? Please click the below button!

Follow MindStick