asp.net-mvc 仅来自 TextBoxFor() 的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1961114/
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
Date only from TextBoxFor()
提问by Kronos
I'm having trouble displaying the only date part of a DateTime into a textbox using TextBoxFor<,>(expression, htmlAttributes).
我无法使用 TextBoxFor<,>(expression, htmlAttributes) 将 DateTime 的唯一日期部分显示到文本框中。
The model is based on Linq2SQL, field is a DateTime on SQL and in the Entity model.
该模型基于 Linq2SQL,字段是 SQL 和实体模型中的 DateTime。
Failed:
失败的:
<%= Html.TextBoxFor(model => model.dtArrivalDate, String.Format("{0:dd/MM/yyyy}", Model.dtArrivalDate))%>
This trick seems to be depreciated, any string value in the object htmlAttribute is ignored.
这个技巧似乎被贬低了,对象 htmlAttribute 中的任何字符串值都被忽略了。
Failed:
失败的:
[DisplayFormat( DataFormatString = "{0:dd/MM/yyyy}" )]
public string dtArrivalDate { get; set; }
I would like to store and display only the date part on the details/edit view, without the "00:00:00" part.
我只想在详细信息/编辑视图中存储和显示日期部分,而不是“00:00:00”部分。
采纳答案by Kevin Craft
[DisplayName("Start Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime StartDate { get; set; }
Then:
然后:
<%=Html.EditorFor(m => m.StartDate) %>
回答by Ross McNab
MVC4has solved this problem by adding a new TextBoxForoverload, which takes a string format parameter. You can now simply do this:
MVC4通过添加一个新的TextBoxFor重载解决了这个问题,它接受一个字符串格式参数。您现在可以简单地执行以下操作:
@Html.TextBoxFor(m => m.EndDate, "{0:d MMM yyyy}")
There's also an overload that takes html attributes, so you can set the CSS class, wire up datepickers, etc:
还有一个采用 html 属性的重载,因此您可以设置 CSS 类、连接日期选择器等:
@Html.TextBoxFor(m => m.EndDate, "{0:d MMM yyyy}", new { @class="input-large" })
回答by Alexeyss
<%= Html.TextBoxFor(model => model.EndDate, new { @class = "jquery_datepicker", @Value = Model.EndDate.ToString("dd.MM.yyyy") })%>
回答by andersjanmyr
Or use the untyped helpers:
或者使用无类型的助手:
<%= Html.TextBox("StartDate", string.Format("{0:d}", Model.StartDate)) %>
回答by user5240713
This worked for me.
这对我有用。
@Html.TextBoxFor(m => m.DateOfBirth, "{0:MM/dd/yyyy}", new { size = "12", @class = "DOB", tabindex = 121 })
回答by Tamas Czinege
Don't be afraid of using raw HTML.
不要害怕使用原始 HTML。
<input type="text" value="<%= Html.Encode(Model.SomeDate.ToShortDateString()) %>" />
回答by Legends
TL;DR;
TL; 博士;
@Html.TextBoxFor(m => m.DOB,"{0:yyyy-MM-dd}", new { type = "date")
Applying [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]didn't work out for me!
申请[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]对我没用!
Explanation:
解释:
In order to display a date property of your model using Html.TextBoxFor:
为了使用以下方法显示模型的日期属性Html.TextBoxFor:
Date property of your model class:
您的模型类的日期属性:
public DateTime DOB { get; set; }
Nothing else is needed on the model side.
模型方面不需要其他任何东西。
In Razor you do:
在 Razor 中,您可以:
@Html.TextBoxFor(m => m.DOB,"{0:yyyy-MM-dd}", new { type = "date")
Explanation:
解释:
The date of an html inputelement of type datemust be formatted in respect to ISO8601, which is: yyyy-MM-dd
input类型的 html元素的日期date必须根据ISO8601进行格式化,即:yyyy-MM-dd
The displayed date is formatted based on the locale of the user's browser, but the parsed value is always formatted yyyy-mm-dd.
显示的日期根据用户浏览器的区域设置格式,但解析的值始终格式为 yyyy-mm-dd。
My experience is, that the language is not determined by the Accept-Languageheader, but by either the browser display language or OS system language.
我的经验是,语言不是由Accept-Language标题决定的,而是由浏览器显示语言或操作系统系统语言决定的。
回答by AaronLS
You can also use the HTML 5 attributes by applying this data annotation:
您还可以通过应用此数据注释来使用 HTML 5 属性:
[DataType(DataType.Date)]
But the problem is this enables a browser specific date picker for HTML 5 browsers. You still need your own date picker for browsers without support, and then you have to make sure your date picker doesn't appear in addition to a browser's (Modernizr can do this easily), or hide the browser's if it does(complicated and I don't know how reliable methods I saw were).
但问题是这为 HTML 5 浏览器启用了浏览器特定的日期选择器。对于没有支持的浏览器,您仍然需要自己的日期选择器,然后您必须确保除了浏览器的日期选择器之外不会出现您的日期选择器(Modernizr 可以轻松做到这一点),或者隐藏浏览器的日期选择器(复杂而我不知道我看到的方法有多可靠)。
In the end I went with Alex's because my current environment doesn't have Modernizr, but if it did, I would have used that to conditionally only show my data picker if the browser didn't support one already.
最后我选择了 Alex 的,因为我当前的环境没有 Modernizr,但如果有,我会用它来有条件地只在浏览器不支持的情况下显示我的数据选择器。
回答by ScubaSteve
For me, I needed to keep the TextboxFor()because using EditorFor()changes the input type to date. Which, in Chrome, adds a built in date picker, which screwed up the jQuery datepicker that I was already using. So, to continue using TextboxFor()AND only output the date, you can do this:
对我来说,我需要保留TextboxFor()因为 usingEditorFor()更改了输入类型。其中,在 Chrome 中,添加了一个内置的日期选择器,它搞砸了我已经在使用的 jQuery 日期选择器。因此,要继续使用TextboxFor()AND 仅输出日期,您可以这样做:
<tr>
<td class="Label">@Html.LabelFor(model => model.DeliveryDate)</td>
@{
string deliveryDate = Model.DeliveryDate.ToShortDateString();
}
<td>@Html.TextBoxFor(model => model.DeliveryDate, new { @Value = deliveryDate }) *</td>
<td style="color: red;">@Html.ValidationMessageFor(model => model.DeliveryDate)</td>
</tr>
回答by CrnaStena
Keep in mind that display will depend on culture. And while in most cases all other answers are correct, it did not work for me. Culture issue will also cause different problems with jQuery datepicker, if attached.
请记住,展示将取决于文化。虽然在大多数情况下所有其他答案都是正确的,但它对我不起作用。如果附加了 jQuery datepicker,文化问题也会导致不同的问题。
If you wish to force the format escape /in the following manner:
如果您希望/通过以下方式强制格式转义:
@Html.TextBoxFor(model => model.dtArrivalDate, "{0:MM\/dd\/yyyy}")
If not escaped for me it show 08-01-2010vs. expected 08/01/2010.
如果没有为我逃脱,它会显示08-01-2010与预期的对比08/01/2010。
Also if not escaped jQuery datepicker will select different defaultDate, in my instance it was May 10, 2012.
此外,如果没有转义 jQuery datepicker 将选择不同的 defaultDate,在我的实例中它是May 10, 2012.


