ASP.NET MVC中MVC ModelMetaData属性如何处理日期格式化问题?

2026-03-30 12:561阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计240个文字,预计阅读时间需要1分钟。

ASP.NET MVC中MVC ModelMetaData属性如何处理日期格式化问题?

我尝试使用MVC3+RC2和DataAnnotations装饰的模型格式化日期。这是我的模型:

csharppublic class Model{ [Display(Name=Date of Birth)] [DisplayFormat(@DataFormatString=MM/dd/yyyy)] public DateTime D { get; set; }}

我试图弄清楚如何使用MVC3 RC2和使用DataAnnotations修饰的模型格式化日期.

这是我的模特……

public class Model { [Display(Name = "Date of Birth")] [DisplayFormat(@DataFormatString = "MM/dd/yyyy")] public DateTime DateOfBirth { get; set; } }

这是我的控制器……

public class IndexController : Controller { public ActionResult Index() { return View( new Model() {DateOfBirth = DateTime.Now}); } }

这是我的看法……

@model MvcApplication6.Models.Model @Html.LabelFor(m=>m.DateOfBirth) @Html.TextBoxFor(m => m.DateOfBirth) @Html.EditorFor(m => m.DateOfBirth)

当我运行该站点时,对于两个控件,页面显示一个文本框,其中包含时间字符串,当我想要的只是日期(由模型中的装饰属性指定).

我在ModelMetaData上使用了Brad Wilson的article这种模式.

我做错了很明显吗?

做这个:

[DisplayName("Date of Birth")] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] public DateTime DateOfBirth { get; set; }

ASP.NET MVC中MVC ModelMetaData属性如何处理日期格式化问题?

本文共计240个文字,预计阅读时间需要1分钟。

ASP.NET MVC中MVC ModelMetaData属性如何处理日期格式化问题?

我尝试使用MVC3+RC2和DataAnnotations装饰的模型格式化日期。这是我的模型:

csharppublic class Model{ [Display(Name=Date of Birth)] [DisplayFormat(@DataFormatString=MM/dd/yyyy)] public DateTime D { get; set; }}

我试图弄清楚如何使用MVC3 RC2和使用DataAnnotations修饰的模型格式化日期.

这是我的模特……

public class Model { [Display(Name = "Date of Birth")] [DisplayFormat(@DataFormatString = "MM/dd/yyyy")] public DateTime DateOfBirth { get; set; } }

这是我的控制器……

public class IndexController : Controller { public ActionResult Index() { return View( new Model() {DateOfBirth = DateTime.Now}); } }

这是我的看法……

@model MvcApplication6.Models.Model @Html.LabelFor(m=>m.DateOfBirth) @Html.TextBoxFor(m => m.DateOfBirth) @Html.EditorFor(m => m.DateOfBirth)

当我运行该站点时,对于两个控件,页面显示一个文本框,其中包含时间字符串,当我想要的只是日期(由模型中的装饰属性指定).

我在ModelMetaData上使用了Brad Wilson的article这种模式.

我做错了很明显吗?

做这个:

[DisplayName("Date of Birth")] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] public DateTime DateOfBirth { get; set; }

ASP.NET MVC中MVC ModelMetaData属性如何处理日期格式化问题?