admin管理员组文章数量:1332403
I am trying to do a very simple task: get an MVC model, and send it back to server as JSON. I tried
@Html.Raw(Json.Encode(Model));
When debugging the JS, I see that the date objects on the serialized JSON look like: /date (00064321)/
and when passing the serialized JSON to the server, the dates are null on the server-side. Anyone understand what is going on?
I am trying to do a very simple task: get an MVC model, and send it back to server as JSON. I tried
@Html.Raw(Json.Encode(Model));
When debugging the JS, I see that the date objects on the serialized JSON look like: /date (00064321)/
and when passing the serialized JSON to the server, the dates are null on the server-side. Anyone understand what is going on?
- Post your ajax code and action method so we can see what is going on – CD Smith Commented May 30, 2012 at 12:15
1 Answer
Reset to default 5Instead of JSON encoding the model directly you have to create an anonymous object converting the date-time properties to strings.
Ex.
var meeting = new Meeting
{
Name = "Project Updates",
StartDateTime = DateTime.Now
};
Passing directly the model..
@Html.Raw(Json.Encode(meeting))
produces
{"Name":"Project Updates","StartDateTime":"\/Date(1338381576306)\/"}
and
@Html.Raw(Json.Encode(new {
Name = meeting.Name,
StartDateTime = meeting.StartDateTime.ToString()
}))
produces
{"Name":"Project Updates","StartDateTime":"5/30/2012 6:09:36 PM"}
as expected.
本文标签: javascriptSerialize MVC model to JSONStack Overflow
版权声明:本文标题:javascript - Serialize MVC model to JSON - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742258008a2441983.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论