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?


基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01