天天看点

JavaScriptSerializer中日期序列化解决方案

后台代码:

JavaScriptSerializer _jsSerializer = new JavaScriptSerializer();   
ViewBag.ProcName = ProcInst.ProcInstName;
            ViewBag.Files = Files;
            var model =
                 Files.Select
                 (
                 p => new { p.UploadTime }
                 ).ToList();

            var jsonStr = _jsSerializer.Serialize(model);
            jsonStr = System.Text.RegularExpressions.Regex.Replace(jsonStr, @"\\/Date\((\d+)\)\\/", match =>
            {
                DateTime dt = new DateTime(1970, 1, 1);
                dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
                dt = dt.ToLocalTime();
                return dt.ToString("yyyy-MM-dd HH:mm:ss");
            });
            ViewBag.FilesJson = jsonStr;      

 前台代码:

<input type="hidden" id="fileIssueData" value="@ViewBag.FilesJson" />      
@foreach (var item in Files.OrderBy(x => x.FileName))
                                            {
                                                <tr>
                                                    <td><img src='/Content/images/@item.FileExName()' /><a href="@Url.Action("DownLoadInfoFile", "FileUpload", new ToAssignFileEntity { ID = item.ID })">@item.FileName</a></td>
                                                    <td>@Convert.ToDateTime(item.UploadTime).ToString("yyyy-MM-dd")</td>
                                                </tr>
                                            }      

 Ajax:

$.ajax({
                url: '@Url.Action("")',
                data: { ""UpFiles": JSON.stringify($.parseJSON($("#fileIssueData").val())),  },
                type: "POST",
                async: false,
                dataType: "json",
                success: function (result) {
                    if (result.Success == true) {
                        func();
                    } else {
                        alert("发起失败:" + result.Msg);
                        return false;
                    };
                    return false;
                }
            });      

继续阅读