在 ASP MVC 中保存后如何防止视图中日期的格式更改
How to prevent format change of date in view after save in ASP MVC
在控制器中保存 DateTime
后,我将它传递回 View
但是当它显示在视图中时,值变为 /Date(1545062400000)/
。
我已经在处理过程中检查了控制器是否更改了数据,但它没有,因为我只是在 view
中传递 viewmodel
。
[HttpPost]
public ActionResult UpdateHeader(RecordViewModel recordViewModel)
{
var ResultMessage = "";
if (ModelState.IsValid)
{
Record record = (from c in context.Records
where c.RecordId == recordViewModel.RecordId
select c).FirstOrDefault();
if (record == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
record.Description = recordViewModel.Description;
record.Date = recordViewModel.Date;
record.Remarks = recordViewModel.Remarks;
try
{
context.SaveChanges();
ResultMessage = "Record successfully updated.";
}
catch (Exception ex)
{
ModelState.AddModelError("", ErrorHelper.GetInnerException(ex));
ResultMessage = "Error: " + ErrorHelper.GetInnerException(ex);
}
}
var result = new { Model = recordViewModel, ResultMessage = ResultMessage };
return Json(result, JsonRequestBehavior.AllowGet);
}
Angular
self.submitEdit = function () {
var updateRecordHeader = RecordServices.updateRecordHeader(self.header)
.then(function successCallback(response) {
self.header = response.data.Model;
self.header.ResultMessage = response.data.ResultMessage;
}, function errorCallback(response) { toastr.error(response.statusText); });
}
自 RFC7519 "epoch" (January 01, 1970), which cannot be directly consumed as JS Date
object without converting it first. The reason behind usage of the ticks is JSON format doesn't have specific representation for DateTime
struct when serialized to plain strings (see this reference 以来,/Date(1545062400000)/
被称为使用 UNIX 时间戳格式的日期刻度。
您可以创建自定义函数将报价转换为 JS Date
对象:
function toJSDate(value) {
var regex = /Date\(([^)]+)\)/;
var results = regex.exec(value);
var date = new Date(parseFloat(results[1])); // or parseInt
return date;
}
或者更简单,无需正则表达式,直接选择数值,如下所示:
function toJSDate(value) {
var date = new Date(parseFloat(value.substring(6))); // or parseInt
return date;
}
然后使用该函数进行即时报价转换:
// example
var header = response.data.Model;
var dateObject = toJSDate(header.Date);
// assign date object afterwards
请注意,您可能需要创建另一个类似于 response.data.Model
的对象结构,但使用服务器端 Date
属性 和 JS 日期对象。
作为替代方案,您可以创建一个 getter-only string
属性,它使用 ToString()
将 DateTime
值转换为所需的字符串表示形式,然后在 JS 中使用它。
旁注:
为了清楚起见,避免使用与内置 JS 函数名称和对象(即 Date
)完全匹配的视图模型 属性 名称。
相关问题:
How do I format a Microsoft JSON date?
Converting .NET DateTime to JSON
在控制器中保存 DateTime
后,我将它传递回 View
但是当它显示在视图中时,值变为 /Date(1545062400000)/
。
我已经在处理过程中检查了控制器是否更改了数据,但它没有,因为我只是在 view
中传递 viewmodel
。
[HttpPost]
public ActionResult UpdateHeader(RecordViewModel recordViewModel)
{
var ResultMessage = "";
if (ModelState.IsValid)
{
Record record = (from c in context.Records
where c.RecordId == recordViewModel.RecordId
select c).FirstOrDefault();
if (record == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
record.Description = recordViewModel.Description;
record.Date = recordViewModel.Date;
record.Remarks = recordViewModel.Remarks;
try
{
context.SaveChanges();
ResultMessage = "Record successfully updated.";
}
catch (Exception ex)
{
ModelState.AddModelError("", ErrorHelper.GetInnerException(ex));
ResultMessage = "Error: " + ErrorHelper.GetInnerException(ex);
}
}
var result = new { Model = recordViewModel, ResultMessage = ResultMessage };
return Json(result, JsonRequestBehavior.AllowGet);
}
Angular
self.submitEdit = function () {
var updateRecordHeader = RecordServices.updateRecordHeader(self.header)
.then(function successCallback(response) {
self.header = response.data.Model;
self.header.ResultMessage = response.data.ResultMessage;
}, function errorCallback(response) { toastr.error(response.statusText); });
}
自 RFC7519 "epoch" (January 01, 1970), which cannot be directly consumed as JS Date
object without converting it first. The reason behind usage of the ticks is JSON format doesn't have specific representation for DateTime
struct when serialized to plain strings (see this reference 以来,/Date(1545062400000)/
被称为使用 UNIX 时间戳格式的日期刻度。
您可以创建自定义函数将报价转换为 JS Date
对象:
function toJSDate(value) {
var regex = /Date\(([^)]+)\)/;
var results = regex.exec(value);
var date = new Date(parseFloat(results[1])); // or parseInt
return date;
}
或者更简单,无需正则表达式,直接选择数值,如下所示:
function toJSDate(value) {
var date = new Date(parseFloat(value.substring(6))); // or parseInt
return date;
}
然后使用该函数进行即时报价转换:
// example
var header = response.data.Model;
var dateObject = toJSDate(header.Date);
// assign date object afterwards
请注意,您可能需要创建另一个类似于 response.data.Model
的对象结构,但使用服务器端 Date
属性 和 JS 日期对象。
作为替代方案,您可以创建一个 getter-only string
属性,它使用 ToString()
将 DateTime
值转换为所需的字符串表示形式,然后在 JS 中使用它。
旁注:
为了清楚起见,避免使用与内置 JS 函数名称和对象(即 Date
)完全匹配的视图模型 属性 名称。
相关问题:
How do I format a Microsoft JSON date?
Converting .NET DateTime to JSON