简体   繁体   English

C#MVC razor datatype.time编辑器在编辑时未以正确的格式显示存储的数据

[英]c# mvc razor datatype.time editorfor not showing stored data in right format on editing

I have a Time datatype property field in a entity: 我在实体中有一个Time数据类型属性字段:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh:mm}")]
[DataType(DataType.Time)]
public DateTime BeginTime { get; set; }

In razor view I have this code to creating and editing 在razor视图中,我有以下代码可以创建和编辑

@Html.EditorFor(model => model.BeginTime, new { htmlAttributes = new { @class = "form-control" } })

On creating i can fill this field in right format with a timepicker, for example 01:15 pm and on database it is stored in right datetime format "2017-10-12 13:15:00.000". 创建时,我可以使用正确的时间选择器(例如01:15 pm)填充此字段,并在数据库中以正确的日期时间格式“ 2017-10-12 13:15:00.000”存储该字段。

But on editing it shows 01:15 am And if i save again the object it modifies field with 01:15 am on database damaging data ("2017-10-12 01:15:00.000"). 但是在编辑时会显示上午01:15,如果我再次保存该对象,它将在数据库破坏性数据(“ 2017-10-12 01:15:00.000”)上以凌晨01:15修改字段。

I have tried this: 我已经试过了:

Set display format of the property in "hh:mm tt" 在“ hh:mm tt”中设置属性的显示格式

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh:mm tt}")]
[DataType(DataType.Time)]
public DateTime BeginTime { get; set; }

But on editing it takes a empty value for the field like (--:-- ---) deleting saved data. 但是在编辑时,它会为(-:----)字段删除一个已保存的数据,而为空值。

Anyone knows how to fix this format issue on editing view? 有人知道如何在编辑视图时解决此格式问题吗?

try this:- 尝试这个:-

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm}")] [DisplayFormat(ApplyFormatInEditMode = true,DataFormatString =“ {0:HH:mm}”))

For more info:- hh vs HH :- Difference between java HH:mm and hh:mm on SimpleDateFormat 有关更多信息:-hh vs HH: -SimpleDateFormat上的Java HH:mm和hh:mm之间的区别

Hope it will work. 希望它能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM