How do you format a DateTime that#39;s displayed by TextBoxFor() in MVC3?(如何格式化 MVC3 中 TextBoxFor() 显示的 DateTime?)
问题描述
我已经安装了 ASP.NET MVC3.我需要带有格式化日期的日期选择器.我试过这个,但它不起作用(当传递{0:dd/MM/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);
}
如果格式为{0:dd-MM-yyyy}",我的代码有效,但不仅适用于{0:dd/MM/yyyy}"
My code works if format is "{0:dd-MM-yyyy}" but not only for "{0:dd/MM/yyyy}"
我知道 MVC4 已经有这个功能,但不幸的是我的项目是在 MVC3 上编写的.你能帮帮我吗?
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何格式化 MVC3 中 TextBoxFor() 显示的 DateTime?


- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 使用 rss + c# 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01