我安装了ASP.NET MVC3。我需要带格式日期的日期选择器。我想这一点,但它不工作(路过的时候“{0:DD/MM/YYYY}”为格式的参数,它仍然不格式):如何格式化MVC3中由TextBoxFor()显示的DateTime?
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);
}
编辑:我的代码工作,如果格式为“{ 0:dd-MM-yyyy}“,但不仅是”{0:dd/MM/yyyy}“
我知道MVC4已经具备了这个功能,但不幸的是我的项目是写在MVC3上的。你可以帮我吗?
可能重复的[DateFormat from TextBoxFor()](http://stackoverflow.com/questions/1961114/date-only-from-textboxfor) – KyleMit