ASP MVC EditorFor DateTime不显示 [英] asp mvc EditorFor DateTime not displaying
问题描述
我试图显示从剃刀文件中的数据库检索到的日期时间字段,内容如下:
I am trying to display a datetime field retrieved froma database in my razor file with the following:
@Html.EditorFor(model => model.RequestDate);
我以100%的确定性知道RequestDate不为null,因为我检查了在视图中传递的模型,它是来自数据库的日期.但是,日期时间文本框仍显示"mm/dd/yyyy".我不知道为什么它不显示日期.有任何想法吗?谢谢.
I know with 100% certainty the RequestDate is not null, as I examined the model that was passed in the view, and it was the date from the database. However, the datetime textbox still displays "mm/dd/yyyy". I don't know why it is not display the date. Any ideas? Thank you.
这是我的模特:
[Display(Name = "Time")]
[DataType(DataType.Date)]
public DateTime RequestDate { get; set; }
[HttpGet]
public ActionResult DispatchResponseIndex()
{
DispatchResponseModel model = new DispatchResponseModel();
//if the users session id isnt null then we know they have data related to this form
object UserID = Session["TestID"];
if (UserID == null)
{
return View(model); //default view
}
else
{
UserID = Session["TestID"];
}
LoadDB(model, (Guid)UserID);
//looking at the model here the RequestDate has a valid date of //{5/24/2013 12:00:00 AM}
return View(model);
}
如果我使用@ Html.TextBox("asdasd",Model.RequestDate);然后它将以字符串格式显示保存的日期,但是我需要使用内置的日期编辑器作为文本框.
If I use @Html.TextBox("asdasd",Model.RequestDate); Then it will display the saved date in string format, but I need to use the built in date editor for the text box.
推荐答案
这似乎确实引起了很多混乱,并且使我烦恼!
This does seem to cause a lot of confusion and it annoyed me!
如果使用 DataType.Date
,则需要这样设置:
If you use DataType.Date
you need to set it like this:
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
Chrome的日期选择器使用本地计算机的短日期格式.即使设置 lang
标签也会被忽略;这是使用Chrome和< html lang ="zh-CN">
的示例:
Chrome's date picker uses the local computer's short date format. Even setting the lang
tag is ignored; here is an example using Chrome and <html lang="en-GB">
:
Chrome的日期选择器还使用本地计算机的语言环境.在页面加载时,将填充日期(如果已设置),并且用户可以选择新日期:
Chrome's date picker also uses the local computer's locale. On page load, the date will be populated (if set) and the user can select a new date:
注意:如果您更改计算机的区域设置,则必须完全重启Chrome才能看到区域设置更改.
Note: If you change the computer's locale you must restart Chrome completely to see the locale change.
这篇关于ASP MVC EditorFor DateTime不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!