0

i have tried the solution proposed in this solution

i have a class

public class MyClass
{
    [IgnoreDataMember]
    public DateTime? Date { get; set; }
}

when the JavaScriptSerializer serializes the result it changes the date time for example if i have 2012-07-20 in my database after the serialization it is returned as 2012-07-21 because the application is hosted at a location that is outside my time zone i am facing a lot of problems because of it

Community
  • 1
  • 1
John x
  • 4,031
  • 8
  • 42
  • 67

1 Answers1

2

You could use the [ScriptIgnore] attribute to exclude properties from being serialized with the JavaScriptSerializer:

public class MyClass
{
    [ScriptIgnore]
    public DateTime? Date { get; set; }

    ... some other properties
}

This being said, the correct way to do this is to use a view model that simply won't contain the property that you don't want to be contained in the response and then return this view model instead of torturing your domain models with plumbing code about serialization which they should be absolutely agnostic of.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • thanks Darin for replying, i'm already using viewmodel and i need to display the date thats y it was included in the model, also i have tried the `[ScriptIgnore]' option when i do that i think i exclude the prop form the resulting json produced. why iam trying to ignore the datetime property is i am suspecting that when it is serialized it changes my date to some utc format that adds an offset of 1 day in my current date... – John x Jul 20 '12 at 06:55
  • I don't understand what you are talking about. Please provide a full example (Model, Controller, View) illustrating your problem. Ideally hardcode values so that I don't need to create a database or something in order to reproduce the issue. – Darin Dimitrov Jul 20 '12 at 07:05
  • its difficult to cut out the relavant code, let me explain the problem, i am in the GMT +5 zone, i have hosted my application in USA that is in different time zone, i have handled the datetime conversion in my code but the problem is when i serialize the result that contains the datetime the serializer converts it in the UTC time stamp, the actual date in the database is '2012-07-20' but when i convert it into json using `JavaScriptSerializer` it converts it into utc time stamp which becomes equivalent to something like `1342869568` i tried converting my time to utc, no efect – John x Jul 20 '12 at 11:25
  • ... so when in my view i deserialize json in the view it gives me `2012-07-21` – John x Jul 20 '12 at 11:26