在asp.net mvc 4中格式化datetime

如何在asp.net mvc 4中强制使用datetime格式?在显示模式下,它显示为我想要的,但在编辑模型中,它没有显示。我正在使用displayfor和editorfor,并使用dataformatstring =“ {0:dd / MM / yyyy}”来applyformatineditmode = true进行了尝试:


我的文化和uiculture在web.config(两者)中都实现了全球化。

在application_start()中修改文化和uiculture

用于日期时间的自定义modelbinder

我不知道如何强制执行,我需要输入日期作为dd / MM / yyyy而不是默认值。


更多信息:我的视图模型是这样的


    [DisplayName("date of birth")]

    [DataType(DataType.Date)]

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]

    public DateTime? Birth { get; set; }

在我看来,@Html.DisplayFor(m=>m.Birth)但是这可以按预期工作(我看到格式)并输入我使用的日期,@Html.EditorFor(m=>m.Birth)但是如果我尝试输入类似13/12/2000的内容,则会失败,并显示错误的日期(12 / 13/2000和2000/12/13正常工作,但我需要dd / MM / yyyy)。


自定义modelbinder在application_start()b / c中调用,我不知道在其他地方。


使用<globalization/>我尝试过的culture="ro-RO", uiCulture="ro"和其他会给我dd / MM / yyyy的文化。我也尝试在application_start()中基于每个线程设置它(这里有很多示例,关于如何执行此操作)


对于所有将要阅读的问题:只要我没有客户端验证,Darin Dimitrov的答案似乎就会起作用。另一种方法是使用定制验证,包括客户端验证。我很高兴在重新创建整个应用程序之前就发现了这一点。


杨魅力
浏览 719回答 3
3回答

犯罪嫌疑人X

啊,现在很清楚。您似乎在绑定值时遇到问题。不能在视图上显示它。确实,这是默认模型绑定程序的错误。您可以编写并使用一种定制[DisplayFormat]模型,该模型将考虑模型上的属性。我已经在这里说明了这样的自定义模型活页夹:https : //stackoverflow.com/a/7836093/29407显然,一些问题仍然存在。这是我的完整设置,可以在ASP.NET MVC 3和4 RC上正常运行。模型:public class MyViewModel{&nbsp; &nbsp; [DisplayName("date of birth")]&nbsp; &nbsp; [DataType(DataType.Date)]&nbsp; &nbsp; [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]&nbsp; &nbsp; public DateTime? Birth { get; set; }}控制器:public class HomeController : Controller{&nbsp; &nbsp; public ActionResult Index()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return View(new MyViewModel&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Birth = DateTime.Now&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; [HttpPost]&nbsp; &nbsp; public ActionResult Index(MyViewModel model)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return View(model);&nbsp; &nbsp; }}视图:@model MyViewModel@using (Html.BeginForm()){&nbsp; &nbsp; @Html.LabelFor(x => x.Birth)&nbsp; &nbsp; @Html.EditorFor(x => x.Birth)&nbsp; &nbsp; @Html.ValidationMessageFor(x => x.Birth)&nbsp; &nbsp; <button type="submit">OK</button>}在以下位置注册自定义模型活页夹Application_Start:ModelBinders.Binders.Add(typeof(DateTime?), new MyDateTimeModelBinder());以及自定义模型活页夹本身:public class MyDateTimeModelBinder : DefaultModelBinder{&nbsp; &nbsp; public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var displayFormat = bindingContext.ModelMetadata.DisplayFormatString;&nbsp; &nbsp; &nbsp; &nbsp; var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);&nbsp; &nbsp; &nbsp; &nbsp; if (!string.IsNullOrEmpty(displayFormat) && value != null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DateTime date;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; displayFormat = displayFormat.Replace("{0:", string.Empty).Replace("}", string.Empty);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // use the format specified in the DisplayFormat attribute to parse the date&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (DateTime.TryParseExact(value.AttemptedValue, displayFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return date;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bindingContext.ModelState.AddModelError(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bindingContext.ModelName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string.Format("{0} is an invalid date format", value.AttemptedValue)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return base.BindModel(controllerContext, bindingContext);&nbsp; &nbsp; }}现在,无论您在web.config(<globalization>元素)中设置了哪种区域性还是当前的线程区域性,自定义模型绑定程序DisplayFormat在解析可为空的日期时都将使用属性的日期格式。
打开App,查看更多内容
随时随地看视频慕课网APP