0

I get the following exception when posting my JSON object back to Asp.net MVC:

"The value '/Date(251071200000)/' is not valid for Date Of Birth."

Looking in Firebug there's definitely a value being posted back so I assume there's some problem in model binding. If I track the DateOfBirth property's set section the 'value' is null.


The Setup Is

C# POCO object with a DateOfBirth property as follows:

    public DateTime? DateOfBirth
    {
        get
        {
            return ClientDto.Contact.DateOfBirth;
        }
        set
        {
            ClientDto.Contact.DateOfBirth = value;
        }
    }

The controller action sends the JSON data to the calling AJAX function as follows (Note: we've tried two options here): Option 1:

/*...extract and initialize a profile object...*/
return Json(profile, JsonRequestBehavior.AllowGet);

Option 2:

/*...extract and initialize a profile object...*/
return Json(new JavaScriptSerializer().Serialize(profile), JsonRequestBehavior.AllowGet);

We post the JSON data back from the client side as follows:

$.ajax({
    url: this.editForm.prop("action"),

    data: kendo.stringify(copy),  //can be replaced with JSON.stringify

    type: "POST",

    contentType: "application/json; charset=utf-8",

    success: function (args)
    {
        //done
    }
});

1 Answer 1

1

I am not very clear on the question but assuming you do get a value back to MVC and, if the error is related to the JSON date format i.e. '/Date(251071200000)/', then i had the same problem.

Basically what you are passing is JSON format of Date. You need to convert date (either on client side or server) to a proper date format.

I used the function below to convert "/Date(210355200000)/" to "1/9/1976"

function fromJsonToJavaScriptDate(value) {
var pattern = /Date\(([^)]+)\)/;
var results = pattern.exec(value);
var dt = new Date(parseFloat(results[1]));
return dt.getDate() + "/" + (dt.getMonth() + 1) + "/" + dt.getFullYear();}

you can use this function on client like ;

$dob = fromJsonToJavaScriptDate([your date value])

you can find the way to convert this json date format on server as explained in the link below.

http://www.developer.com/net/dealing-with-json-dates-in-asp.net-mvc.html

I hope this helps.

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

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.