C# 你如何格式化由 TextBoxFor() 在 MVC3 中显示的日期时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13725057/
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
How do you format a DateTime that's displayed by TextBoxFor() in MVC3?
提问by karaxuna
I have ASP.NET MVC3 installed. I need datepicker with formatted date. I tried this, but it's not working (when passing "{0:dd/MM/yyyy}" as format parameter, it still does not format):
我安装了 ASP.NET MVC3。我需要带格式化日期的日期选择器。我试过这个,但它不起作用(当传递“{0:dd/MM/yyyy}”作为格式参数时,它仍然没有格式化):
private static MvcHtmlString FormattedDateTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string format, RouteValueDictionary htmlAttributes)
{
var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
if (metadata.Model != null && metadata.Model as DateTime? != null)
htmlAttributes.Add("value", string.Format(format, (DateTime)metadata.Model));
return htmlHelper.TextBoxFor(expression, htmlAttributes);
}
EDIT: My code works if format is "{0:dd-MM-yyyy}" but not only for "{0:dd/MM/yyyy}"
编辑:如果格式为“{0:dd-MM-yyyy}”,但不仅适用于“{0:dd/MM/yyyy}”,我的代码也有效
I know that MVC4 has already this functionality, but unfortunately my project is written on MVC3. Can you help me?
我知道MVC4已经有这个功能了,但不幸的是我的项目是在MVC3上写的。你能帮助我吗?
回答by IyaTaisho
In your Model, use the following:
在您的模型中,使用以下内容:
[DisplayFormat(DataFormatString="{0:dd/MM/yyyy}")]
public DateTime YourDate { get; set; }
Here is what it should do in the View:
这是它应该在视图中执行的操作:
@Html.EditorFor(model => model.YourDate, new { @class = "date" })
This should give you a date formatted out to the format of dd/MM/yyyy.
这应该给你一个格式化为 dd/MM/yyyy 格式的日期。
回答by ericbl
The only format I have even gotten to work is this:
我什至开始工作的唯一格式是:
@Html.TextBoxFor(m => m.Birthdate, "{0:MM/dd/yyyy}")
回答by ninetiger
The syntax is: @Html.TextBoxFor( expression, string format, object htmlAttributes)
语法为:@Html.TextBoxFor(表达式,字符串格式,对象htmlAttributes)
eg:
例如:
@Html.TextBoxFor(x => x.DateUtc, "{0:yyyy-MM-dd HH:mm:ss}",
new { @class = "form-control", placeholder = "Enter Title", id="myDate"})
回答by Hassan Shabbir
this format will help you:
这种格式将帮助您:
@Html.TextBoxFor(Model => Model._facadeLowdown.DoB, new { @value= Model._facadeLowdown.DoB.HasValue ? Model._facadeLowdown.DoB.Value.ToString("MM/dd/yyyy") : ""
}
回答by CrnaStena
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.YourDate, "{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.

