如何格式化 MVC3 中 TextBoxFor() 显示的 DateTime? [英] How do you format a DateTime that's displayed by TextBoxFor() in MVC3?

查看:18
本文介绍了如何格式化 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆