格式化的日期TextBoxFor [英] Formatted Date TextBoxFor

查看:115
本文介绍了格式化的日期TextBoxFor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了ASP.NET MVC3。我需要用日期选择器格式的日期。我想这一点,但它不工作(路过的时候{0:DD / MM / YYYY}作为format参数,它仍然不格式):

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);
    }

编辑:我的code工作,如果格式为{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}")

这篇关于格式化的日期TextBoxFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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