开发者

MVC3 Inconsistent JSON/DateTime Behaviour between JsonResult and JsonValueProviderFactory?

I recently upgraded one of my project from MVC2 to MVC3 and adjusted some code accordingly. One issue I enountered is the JSON and DateTime issue.

I have a very simple code to demo, the idea is very straight forward that I return JSON from controller, client side JavaScript receives as is and post back to another action method to compare data.

My view model used as data container is

public class JsonViewModel {
    public int IntegerValue {
        get;
        set;
    }

    public string StringValue {
        get;
        set;
    }

    public DateTime DateTimeValue {
        get;
        set;
    }
}

I have a controller with 2 action methods, one to generate JSON data and one to receive JSON data:

public class HomeController : Controller {
    [HttpPost]
    public JsonResult GetJsonData() {
        JsonViewModel data = new JsonViewModel
        {
            IntegerValue = 99,
            StringValue = "This is test string",
            DateTimeValue = DateTime.Now
        };

        return new JsonResult { ContentEncoding = Encoding.UTF8, Data = data };
    }

    [HttpPost]
    public ActionResult ReceiveJsonData(JsonViewModel data) {
        return View(data);
    }
}

The view code is very simple as well,

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<SomeClass>" %>
<!DOCTYPE html>
<html>
<head runat="server">
<link type="text/css" href="<%: Url.Content("~/Content/site1.css") %>" rel="stylesheet" />
<link type="text/css" href="<%: Url.Content("~/Content/themes/base/jquery.ui.all.css") %>" rel="stylesheet" />
<script type="text/javascript" src="<%: Url.Content("~/Scripts/jquery-1.5.1.js") %>"></script>
<script type="text/javascript" src="<%: Url.Content("~/Scripts/jquery.validate.js") %>"></script>
<script type="text/javascript" src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.js") %>"></script>
<script type="text/javascript" src="<%: Url.Content("~/Scripts/jquery-ui-1.8.11.js") %>"></script>
</head>
<body>
<input type="button" value="JSON Test" id="btnJson" />
<script>
    $(document).ready(function () {
        $('#btnJson').click(function () {
            $.ajax({
                type: "Post",
                url: "/Home/GetJsonData/",
                dataType: "json",
                error: function (request, error) {
                    alert("readyState: " + request.readyState + "\nstatus: " + request.status);
                    alert("responseText: " + request开发者_开发百科.responseText);
                },
                success: function (data) {
                    var jsonData = JSON.stringify(data);
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "/Home/ReceiveJsonData/",
                        cache: false,
                        data: jsonData,
                        dataType: "html",
                        success: function (result) {
                            //alert(result);
                        },
                        error: function (request) {
                            alert("readyState: " + request.readyState + "\nstatus: " + request.status);
                            alert("responseText: " + request.responseText);
                        }
                    });
                }
            });
        });
    });
</script>
</body>
</html>

What I expect is that the object genrated and searilzed in GetJsonData action method should be same as ReceiveJsonData action method. But the actual behaviour is that integer and string values are persisted, but datetime value is reset.

Any clue why?


Or you could do something like this:

var jsonData = JSON.stringify(data).replace(/\/Date\(\d+\)/g, function (a) { return '\\' + a + '\\'; });

And it should work like a charm.


I have the same issue. During tests i've realized that JSON.stringify removes "\" from "\/Date(xxxxx)\/" and MVC is not able to deserialize it. The solution is to enforce different type of DateTime serialization. Instead of returning JsonResult I do something like this: var json = JsonConvert.ExportToString(data); return Content(json,"application/json");

JsonConvert is a Jayrock library class. It stores DateTime values in XML format which is understandable for MVC. Hope that helps


I got an answer from http://forums.asp.net/t/1689952.aspx/1 which work perfectly to me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜