【问题标题】:Automatical bind of datetime fields in MVC3MVC3 中日期时间字段的自动绑定
【发布时间】:2012-06-18 16:28:33
【问题描述】:

我有一个包含许多日期字段的页面,可以在客户端动态添加。我有一个日期时间?此字段的编辑器模板上的属性:

[Display(Name = "Bar Admission Date")]
public DateTime? AdmissionDate { get; set; }

当我提交表单时,AdmissionDate 字段中的数据为空,因为 binder 不知道该字段的格式。

关于如何克服这个问题,我有 2 个想法:

  1. 在模型中创建一个字符串字段并在服务器端解析它。简单而且相当快。
  2. 为日期字段编写自定义模型绑定器。我不喜欢这个解决方案,因为我不知道我将使用的所有字段的键。

有没有更好的解决方案?我搜索了如何重载 TextboxFor 方法以传递文化,但我没有找到

【问题讨论】:

    标签: asp.net-mvc-3 datetime data-binding model


    【解决方案1】:

    听起来您应该使用DateTime? 的可枚举(IList/ICollection)。 Phil Haacked has a good article on model binding to a list (even when the number of items is dynamic).

    更新

    至于格式问题,我看看怎么set the culture for the project/model binder

    【讨论】:

    • 谢谢,已经看过他的文章了。问题只是日期格式。不要在文章中看到这一点
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 2016-12-29
    • 2018-01-29
    • 2011-08-10
    • 2011-06-09
    • 1970-01-01
    相关资源
    最近更新 更多