[英]What determines the DateTime.Kind when ASP.Net receives a posted model with a DateTime property?
We have a number of Vue pages, with Typescript models, that submit to the same controller action.我们有许多 Vue 页面,带有 Typescript 模型,它们提交到相同的 controller 操作。 The controller takes a single complex parameter, and the class features a DateTime property.
controller 采用单个复杂参数,而 class 具有 DateTime 属性。
The Vue pages have been modified over time and now look quite unlike each other. Vue 页面已经随着时间的推移进行了修改,现在看起来彼此完全不同。 However, in all cases the DateTime property is taken from an inputDateOfBirth field and mapped with Moment.
但是,在所有情况下,DateTime 属性都取自 inputDateOfBirth 字段并与 Moment 进行映射。 The code tends to look a bit like this:
代码看起来有点像这样:
var dob = Helpers.ToMoment(this.inputDateOfBirth);
if (dob.isValid()) {
this.dateOfBirth = dob.format("YYYY-MM-DD HH:mm:ss");
}
Both dateOfBirth
and inputDateOfBirth
are listed as type string
in the Typescript model. dateOfBirth
和inputDateOfBirth
在 Typescript model 中都列为类型string
。
In most cases, the complex parameter that ends up being received by the controller action has this DateTime property as DateTime.Kind
of Utc
.在大多数情况下,最终由 controller 操作接收的复杂参数具有此 DateTime 属性作为
DateTime.Kind
of Utc
。 One of them, however, has a DateTime.Kind
of Unspecified
.然而,其中一个有一个
DateTime.Kind
的Unspecified
。 This causes issues further down the line.这会导致进一步的问题。
What determined the DateTime.Kind value of data interpreted by a C# controller action in ASP.NET MVC, and what can I do to ensure it's received consistently as Utc?是什么决定了由 C# controller 操作在 ASP.NET MVC 中解释的数据的 DateTime.Kind 值,我可以做些什么来确保它被一致地接收为 Utc?
Format of the date can causes this difference.日期格式可能会导致这种差异。
Default model binder for DateTime expects ISO 8601 Time zone designators
, which means that if your date string looks like this 2009-06-15T13:45:30.0000000Z
, then DateTime.Kind
will be UTC
as “Z” presented. DateTime 的默认 model 活页夹需要ISO 8601
Time zone designators
,这意味着如果您的日期字符串看起来像这样2009-06-15T13:45:30.0000000Z
,那么DateTime.Kind
将是UTC
,如“Z”所示。
Or if like this 2009-06-15T13:45:30.0000000-07:00
, where timezone offset “-07:00” is presented, DateTime.Kind
will be Local
.或者,如果像这样
2009-06-15T13:45:30.0000000-07:00
出现时区偏移“-07:00”,则DateTime.Kind
将是Local
。
If none of this attributes is presented, DateTime.Kind
will be Unspecified
.如果没有提供这些属性,则
DateTime.Kind
将是Unspecified
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.