views:

70

answers:

2

Here is my jQuery code:

function onSaveClicked()
{
    var message = 
    {
        MessageID: $("#MessageID").val() || 0,
        MessageDate: "\/Date(<%= DateTime.Now.Ticks %>)\/",
    };


    $.ajax({
       url: "<%= Url.Action("SaveMessage") %>",
       type: "POST",
       dataType: "json",
       data: $.toJSON(message),
       contentType: "application/json; charset=utf-8",
       success: function(result) {
            if (result && result.success)
            {
                //
            }
       }
   }); 
}

At first, I was just setting MessageDate to a string that was in a date format, but after some errors there, I did some research and it looks like I need to pass in the Ticks. But I get the following error:

There was an error deserializing the object of type Models.MessageModel. The value '634185025866884281' cannot be parsed as the type 'DateTime'

I've also tried:

MessageDate: "\\/Date(<%= DateTime.Now.Ticks %>)\\/",

but I get this error message:

There was an error deserializing the object of type Models.MessageModel. DateTime content '\/Date(634185027273624742)\/' does not start with '\/Date(' and end with ')\/' as required for JSON.

What do I need to do to get this working?

EDIT: I'm using this to deserialize the JSON request:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    if (filterContext.HttpContext.Request.ContentType.Contains("application/json"))
    {
        var serializer = new DataContractJsonSerializer(RootType);
        //RootType here is (Name = "MessageModel", FullName="Models.MessageModel")

        filterContext.ActionParameters["message"] = serializer.ReadObject(filterContext.HttpContext.Request.InputStream);


    }
}
+1  A: 

I had the same problem. What I did was use the following function to convert it to a number

private double GetUnixEpoch(DateTime dateTime)
    {
        var unixTime = dateTime.ToUniversalTime() -
            new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        return unixTime.TotalMilliseconds;
    }

Then you can use that number in the constructor of the Javascript date object to create the date object.

Mark 909
Can you show me an example of how I'd use that in my Javascript?
Steven
+1  A: 

You may try the following function:

public static string FormatDate(DateTime dt)
{
    var serializer = new DataContractJsonSerializer(typeof(DateTime));
    using (var stream = new MemoryStream())
    {
        serializer.WriteObject(stream, dt);
        return Encoding.Default.GetString(stream.ToArray());
    }
} 

And in your view:

var message = 
{
    MessageID: $("#MessageID").val() || 0,
    MessageDate: "/Date(<%= SomeClass.FormatDate(DateTime.Now) %>)/"
};
Darin Dimitrov
This gave me the following error:There was an error deserializing the object of type Models.MessageModel. The value '/Date(1282924488721' cannot be parsed as the type 'Int64'
Steven
Could you post the JSON string that is passed in your ActionFilter? In your example the contents of `filterContext.HttpContext.Request.InputStream`.
Darin Dimitrov
I read the stream into a byte array, and then converted the byte array to a string. This is the result: "1233477101115115971031017368345848443484105116108101345834116101115116503444346810111599114105112116105111110345834116101115116105110103344434689711610183116971141163458344768971161014047689711610140495056505750565150515654504548524848414741473444346897116101691101003458344768971161014047689711610140495056505750565150515654514548524848414741473444348610511510598108101345810297108115101125"
Steven