TextBoxFor Date对象总是显示时间要素 [英] TextBoxFor Date object always shows time element
问题描述
我有一个问题,显示/编辑MVC 3日期。
I am having a problem displaying/editing a date in MVC 3.
我已经建立了我的数据类属性如下(该数据实际是由LINQ2SQL对象提供):
I have set up my data class property as follows (the data is actually provided by a Linq2Sql object):
[DisplayName("Date of Birth")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}", NullDisplayText = "")]
public DateTime DoB { get; set; }
在视图,然后我有:
@Html.TextBoxFor(m => m.DoB, new { @class = "date" })
的问题是,在文本框中总是显示日期,例如时间部分'18 /二千零十分之一十00:00:00
The problem is, the text box always shows the time portion of the date, e.g. '18/10/2010 00:00:00'
我知道我可以通过使用标准Html.TextBox
I know I can get over this problem by using a standard Html.TextBox
@Html.TextBox("DoB", Model.DoB.ToShortDateString())
但我真的希望能够从数据模型控制这一点。
but I really want to be able to control this from the data model.
我在网络上的显示,这个工程中发现的文章,但我不能重复他们的成功。
I have found articles on the web that suggest that this works, but I can't repeat their success.
任何帮助/意见将是AP preciated。
Any help/advice would be appreciated.
推荐答案
如果你想要的数据注解属性,如 DisplayFormat
有你应该使用任何影响:
If you want the data annotations attributes such as DisplayFormat
to have any effect you should use:
@Html.EditorFor(m => m.DoB)
签出<一个href=\"http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-2-modelmetadata.html\">following从布拉德·威尔逊的博客文章这也解释了模型元数据在ASP.NET MVC是如何工作的。
Checkout the following blog post from Brad Wilson which explains how model metadata works in ASP.NET MVC.
的缺点是,现在你不能再指定类。一个可能的解决办法是要做到这一点:
The drawback is that now you cannot specify the class anymore. One possible solution would be to do this:
<div class="input-date">
@Html.EditorFor(m => m.DoB)
</div>
然后风格:
.input-date input {
...
}
或者你也可以写一<一href=\"http://aspadvice.com/blogs/kiran/archive/2009/11/29/Adding-html-attributes-support-for-Templates-_2D00_-ASP.Net-MVC-2.0-Beta_2D00_1.aspx\">custom DataAnnotationsModelMetadataProvider 这将允许您指定的属性,如类,大小,最大长度,使用...注释数据
or you could also write a custom DataAnnotationsModelMetadataProvider which would allow you to specify attributes such as class, size, maxlength, ... using data annotations.
这篇关于TextBoxFor Date对象总是显示时间要素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!