如何格式化MVC3中由TextBoxFor()显示的DateTime? [英] How do you format a DateTime that's displayed by TextBoxFor() in MVC3?
本文介绍了如何格式化MVC3中由TextBoxFor()显示的DateTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
pre (this HtmlHelper< TModel> htmlHelper,Expression< Func< TModel,TProperty>>表达式,字符串格式,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);
$ b编辑:如果格式为{0:dd-MM- yyyy},但不仅仅是{0:dd / MM / yyyy}
我知道MVC4已经有了这个功能,但不幸的是我的项目是写在MVC3 。你可以帮我吗?
解决方案
我唯一的工作格式是:
@ Html.TextBoxFor(m => m.Birthdate,{0:MM / dd / yyyy})
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):
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}"
I know that MVC4 has already this functionality, but unfortunately my project is written on MVC3. Can you help me?
解决方案
The only format I have even gotten to work is this:
@Html.TextBoxFor(m => m.Birthdate, "{0:MM/dd/yyyy}")
这篇关于如何格式化MVC3中由TextBoxFor()显示的DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文