gpt4 book ai didi

c# - 如何格式化 MVC3 中 TextBoxFor() 显示的 DateTime?

转载 作者:bug小助手 更新时间:2023-10-28 10:46:37 26 4
gpt4 key购买 nike

我已经安装了 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);
}

编辑:如果格式为“{0:dd-MM-yyyy}”,我的代码有效,但不仅适用于“{0:dd/MM/yyyy}”

我知道 MVC4 已经有这个功能,但不幸的是我的项目是在 MVC3 上编写的。你能帮帮我吗?

最佳答案

我什至可以使用的唯一格式是:

@Html.TextBoxFor(m => m.Birthdate, "{0:MM/dd/yyyy}")

关于c# - 如何格式化 MVC3 中 TextBoxFor() 显示的 DateTime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13725057/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com