asp.net-mvc asp mvc EditorFor DateTime 不显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16843411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
asp mvc EditorFor DateTime not displaying
提问by John Edwards
I am trying to display a datetime field retrieved froma database in my razor file with the following:
我正在尝试使用以下内容显示从我的剃刀文件中的数据库中检索到的日期时间字段:
@Html.EditorFor(model => model.RequestDate);
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.
我 100% 肯定地知道 RequestDate 不为空,因为我检查了视图中传递的模型,它是来自数据库的日期。但是,日期时间文本框仍显示“mm/dd/yyyy”。我不知道为什么它不显示日期。有任何想法吗?谢谢你。
Here is my model:
这是我的模型:
[Display(Name = "Time")]
[DataType(DataType.Date)]
public DateTime RequestDate { get; set; }
EDIT:
编辑:
[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);
}
EDIT 2:
编辑2:
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.
如果我使用 @Html.TextBox("asdasd",Model.RequestDate); 然后它将以字符串格式显示保存的日期,但我需要使用文本框的内置日期编辑器。
回答by Piotr Kula
This does seem to cause a lot of confusion and it annoyed me!
这似乎确实引起了很多混乱,这让我很恼火!
If you use DataType.Dateyou need to set it like this:
如果你使用DataType.Date你需要这样设置:
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
Chrome's date picker uses the local computer's short date format.Even setting the langtag is ignored; here is an example using Chrome and <html lang="en-GB">:
Chrome 的日期选择器使用本地计算机的短日期格式。甚至设置lang标签也被忽略;这是一个使用 Chrome 和的示例<html lang="en-GB">:


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.
注意:如果您更改了计算机的区域设置,则必须完全重新启动 Chrome 才能看到区域设置的更改。
回答by John Edwards
FINAL
最终的
I'm an idiot. I had no idea Chrome had a date picker that it added. I thought it was something built into MVC. I need to use an actual jquery date picker. Thanks everyone for all the info.
我是个白痴。我不知道 Chrome 有一个它添加的日期选择器。我认为这是MVC内置的东西。我需要使用实际的 jquery 日期选择器。感谢大家提供的所有信息。
回答by John Edwards
Add Format line in you model
在您的模型中添加格式行
[Display(Name = "Time")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
[DataType(DataType.Date)]
public DateTime RequestDate { get; set; }
Update
更新
OR
或者
Can you change the model property datetime to string. and convert your datetime object to string !!
您能否将模型属性日期时间更改为字符串。并将您的日期时间对象转换为字符串!!
OR
或者
Or you can format that value in your editor field .for sample like
或者您可以在编辑器字段中格式化该值。例如
@Html.TextBoxFor(model => model.FromDate, "{0:d}")
回答by karl li
Actually until I saw this post, I didn't realise there's "date picker" difference between Chrome and IE, even though I got the sample page layout from the MVC's tutorial.
实际上,直到我看到这篇文章,我才意识到 Chrome 和 IE 之间存在“日期选择器”差异,即使我从 MVC 的教程中获得了示例页面布局。
For someone who doesn't realise the difference, as I did before, please find below the comparison:
对于像我之前所做的那样没有意识到差异的人,请在下面找到比较:


回答by Rumesh Silva
This is how i do it inside the model
这就是我在模型中的做法
[Display(Name = "Effective Date")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MMM-yyyy}")]
public DateTime? Effectivedate
{
get { return EffectivedateEdit; }
set { EffectivedateEdit = value; EffectivedateEdit = value; }
}
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime? EffectivedateEdit { get; set; }
回答by Mark
I had this exact same problem. If you look at the HTTP Post that was going back to the Controller and the format of the date in the POST it is yyyy-MM-dd.
我有这个完全相同的问题。如果您查看返回到控制器的 HTTP Post 以及 POST 中的日期格式,它是 yyyy-MM-dd。
I changed my data annotation to this and it worked ... Except now the Index list view shows yyyy-mm-dd format. And the Edit shows mm/mm/yyyy in a datepicker control?
我将我的数据注释更改为这个并且它起作用了......除了现在索引列表视图显示 yyyy-mm-dd 格式。并且编辑在日期选择器控件中显示 mm/mm/yyyy?
Added to Model Date -
添加到模型日期 -
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")
IMHO EVERY app I've seen since 1980 has used dates and times. Why is this still a mess Mr MS?
恕我直言,自 1980 年以来我见过的每个应用程序都使用了日期和时间。为什么这仍然是一团糟 MS 先生?

