[英]DateTime and DateTimeOffset Conversion Issues
我的ModelState總是被設置為無效,因為我的一個模型中的CreatedAt值總是以Dates / ############ /返回,這導致空值。 我想知道為什么會這樣。 CreatedAt是DateTimeOffset。 我相當確定它應該從另一個Database表中提取DateTimeOffset值。
我已經嘗試將它轉換為datetime,創建一個新的DateTimeOffset並將其傳入,並只傳遞DateTimeOffset.Now以使其有效。 沒有人工作過。
public ActionResult EditSafetyIncidentPopUp([DataSourceRequest] DataSourceRequest request, SafetyIncidentViewModel sivm)
{
if (sivm != null && ModelState.IsValid)
{
SafetyIncident si = new SafetyIncident
{
Id = sivm.Id,
CreatedAt = new DateTimeOffset(sivm.CreatedAt),
Description = sivm.Description,
Type = sivm.Type,
ProductionLineId = sivm.ProductionLineId,
ProdLine = _productionLineService.Find(sivm.ProductionLineId)
};
_safetyIncidentService.Update(si);
}
return this.Json(new[] { sivm }.ToDataSourceResult(request,ModelState));}
public class SafetyIncidentViewModel
{
public string Id { get; set; }
[Required]
[ReadOnly(true)]
public DateTime CreatedAt { get; set; }
[Required]
public string Description { get; set; }
[Required]
[Range(0, 1, ErrorMessage = "Enter a valid type.")]
public type Type { get; set; }
[Display(Name = "Production Line")]
[Required]
public string ProductionLineId { get; set; }
[Display(Name = "Production Line")]
public ProductionLine ProdLine { get; set; }
}
我希望結果能夠成功更新我的數據庫。 它目前只更新網格,然后無法將數據傳遞到數據庫,因為ModelState無效。
更改視圖模型以使用日期時間偏移而不是DateTime。 然后就不需要轉換了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.