asp.net-mvc 为什么 DisplayFormat DataFormatString 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16697872/
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
Why is DisplayFormat DataFormatString not working?
提问by ProfK
I have a property in my view model as follows:
我的视图模型中有一个属性,如下所示:
[Editable(false)]
[Display(Name = "Date")]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public DateTime MovementDate { get; set; }
Yet the markup
然而标记
<td>
@Html.DisplayFor(modelItem => item.MovementDate)
</td>
renders the date value as 2013/05/15 12:00:00 AM.
将日期值呈现为2013/05/15 12:00:00 AM.
What am I doing wrong? My model:
我究竟做错了什么?我的型号:
public class WithDateModel
{
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}")]
public DateTime TheDate { get; set; }
public WithDateModel()
{
TheDate = DateTime.Now;
}
}
My view:
我的看法:
@model ParkPay.WebTests.Models.WithDateModel
@Html.DisplayFor(m => m.TheDate)
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
What gets rendered:
什么被渲染:
2013/05/25 02:23:37 AM
采纳答案by MattSull
Why are you using ApplyFormatInEditModeif you have set [Editable(false)]? All ApplyFormatInEditModedoes is format the date if you have it in a text box or something for editing, which you probably won't because of the aforementioned.
ApplyFormatInEditMode如果你设置了你为什么要使用[Editable(false)]?所有ApplyFormatInEditMode做的是格式化的日期,如果你在一个文本框或东西编辑有它,这你可能会在上述的不是因为。
I was able to get the date to display correctly using the following:
我能够使用以下方法正确显示日期:
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}")]
public DateTime Date
{
get
{
return DateTime.Now;
}
set
{
Date = DateTime.Now;
}
}
and in the view (with resulting output):
并在视图中(带有结果输出):
@Html.DisplayFor(x => x.Date) // 2013/05/23
<br />
@Model.Date // 23/05/2013 09:57:56
<br />
@Html.DisplayFor(x => Model.Date) // 2013/05/23
Hope this helps.
希望这可以帮助。
回答by user1616625
If you can't get it working on the model you could try it on the view.
如果你不能让它在模型上工作,你可以在视图上尝试。
@Html.TextBoxFor(m => m.ValidFrom, "{0:dd/MM/yyyy}", new {maxlength = 10})
@Html.TextBoxFor(m => m.ValidFrom, "{0:dd/MM/yyyy}", new {maxlength = 10})
回答by Md. Ilyas Hasan Mamun
Since You want to exclude the time to get Only Date: At Model:-
由于您想排除获取唯一日期的时间:在模型:-
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy/MM/dd}"]
public DateTime TheDate { get; set; }
At Views:-
在视图中:-
@Html.DisplayFor(model=> model.TheDate)
@Html.JQueryUI().DatepickerFor(model => model.TheDate)
The tutorial of the following link may help you.It works for me.
以下链接的教程可能对您有所帮助。它对我有用。
http://ilyasmamunbd.blogspot.com/2014/02/jquery-ui-datepicker-popup-calendar.html
http://ilyasmamunbd.blogspot.com/2014/02/jquery-ui-datepicker-popup-calendar.html
回答by yzorg
In my case a teammate had defined a global display template (aka ~\Views\Shared\DisplayTemplates\DateTime.cshtml) that I didn't realize took precedence over my [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]on my model property.
在我的例子中,一个队友定义了一个全局显示模板(又名~\Views\Shared\DisplayTemplates\DateTime.cshtml),我没有意识到它优先[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]于我的模型属性。
The solution was to move my format string to new shared template, so the attribute changes to:
解决方案是将我的格式字符串移动到新的共享模板,因此属性更改为:
// see ~\Views\Shared\DisplayTemplates\DateMyFormat.cshtml for formatting
[UIHint("DateMyFormat")]
public DateTime MovementDate { get; set; }
回答by Prakash G. R.
You have to annotate the type as Date
您必须将类型注释为日期
[DataType(DataType.Date)]
回答by Enrico
I had a similar issue with a datepicker which was showing the date in the wrong order. Fixed it by using the HTML 5 value field. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date
我有一个日期选择器的类似问题,它以错误的顺序显示日期。使用 HTML 5 值字段修复了它。 https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date
<input asp-for="From" type="text" class="pickadate picker__input" value="@Model?.From.ToString("MM/dd/yyyy")">
回答by Keith Aymar
This work for me:
这对我有用:
In the model:
在模型中:
using System.ComponentModel.DataAnnotations;
namespace Athlete.Models
{
public class Foo
{
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime SignDate
{
get;
set;
}
}
}
And in the view:
在视图中:
<td style="text-align:left;">@Html.DisplayFor(modelItem => item.SignDate)</td>
回答by Grommash Hellscream
You need the var name in the Display Name:
[Display(Name = "MovementDate")]
public DateTime MovementDate{ get; set; }
您需要显示名称中的 var 名称:
[Display(Name = " MovementDate")]
public DateTime MovementDate{ get; 放; }
[Editable(false)]
[Display(Name = "MovementDate")]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public DateTime MovementDate { get; set; }

