【问题标题】:Using the new Model Binding in asp.net 4.5 to format date fields在 asp.net 4.5 中使用新的模型绑定来格式化日期字段
【发布时间】:2012-08-31 15:19:13
【问题描述】:

在 asp.net 4.5 之前的过去,我们可以轻松地将日期绑定到 Gridview、Formview 或其他数据绑定控件,并使用简单的格式字符串使其看起来像样。

            <asp:TextBox 
                ID="DateFieldTextBox" 
                runat="server" 
                Text='<%# Bind("DateField","{0:d}") %>'/>

新的强类型模型绑定有点困难。

            <asp:TextBox 
                ID="DateFieldTextBox" 
                runat="server" 
                Text='<%# BindItem.DateField %>'/>

会起作用,但会生成完整的日期/时间字符串,而不仅仅是日期。

            <asp:TextBox 
                ID="DateFieldTextBox" 
                runat="server" 
                Text='<%# BindItem.DateField.ToShortDateString() %>'/>

.. 这应该可以,但它会产生编译时错误“BindItem 的代码语法无效”。 Item.DateField.ToShortDateString() 有效,但不绑定回发。

现在我们已经恢复到旧语法,但我们希望获得现代编译时间检查,但仍然能够很好地格式化日期。其他人之前遇到过这种情况吗?

【问题讨论】:

  • 在尝试此操作时也不起作用:'&lt;%# string.Format(CultureInfo.CurrentCulture, "{0:d}", BindItem.InfoBirthday) %&gt;',它说:编译器错误消息:CS0103:当前上下文中不存在名称“BindItem”
  • @senthilkumar:你读过这个问题吗?我们需要新模型绑定的替代方案——不要使用旧的Eval(后期绑定反射),而是使用Container.DataItemstring.Format("{0:d}", ((DataRowView)Container.DataItem)["DateField"])

标签: visual-studio-2012 asp.net-4.5


【解决方案1】:

为了在网格视图中显示数据,您可以执行 Item.DateField.ToShortString() ,它会起作用,因为它是一种数据绑定方式。即您正在显示记录中的内容。 BindItem.DateField.ToDateString() 会将对象从 DateTime 转换为字符串,这将导致您已经看到的错误。您可以在编辑字段或接受新条目时在表单视图中继续使用 Bind(expression,format)。

【讨论】:

    【解决方案2】:

    我为此苦苦挣扎了一段时间。我的解决方案是结合数据模型中的数据注释以及在我的 FormView 中使用 DynamicControl。我假设这是一个 WebForms 项目与一个 MCV 项目。

    阅读一下data access in WebForms 的领域。它是 MS 对 WebForms 数据访问/呈现控件的更好描述之一,其中包括对 DynamicControl 的引用。

    //I know you want this, and I did too.  It seems like it could/should work, but it doesn't        
    <asp:TextBox 
         ID="DateFieldTextBox" 
         runat="server" 
         Text='<%# BindItem.DateField.ToShortDateString() %>'/>
    
    //This works, but you also need to use the Data Annotations in your model.
    <asp:DynamicControl
         ID="DateFieldTextBox"
         runat="server"
         DataField="DateField"
         Mode="Edit" />
    
    //sample model annotations in your object class
      [Column(TypeName = "date"), DataType(DataType.Date), DisplayFormat(DataFormatString = "  {0:MM/dd/yyyy}", ApplyFormatInEditMode = true )]
      public DateTime DateField { get; set; }
    

    我将使用这种模式,因为我需要能够以正确格式(以及货币等)显示日期的双向模型绑定。很遗憾,TextBox 控件不能像这样完全处理模型数据的格式。在此上下文中忽略 DataAnnotations。如果没有格式化,双向绑定有效,但如果您需要格式化,则无效。 IMO 似乎是一个不完整的 WebForms 实现。

    另一方面,使用 DynamicControl 是可行的,但您不会在 DataField 属性上获得 Intellisense,该属性会在编码时为您提供模型成员。什么????另一个不完整且不太受欢迎的解决方案。

    【讨论】:

    • 这里的想法对我有用(在转发器中绑定)。使用 DisplayFormat 注释属性并使用 DynamicControl 而不是 BindItem.Bind。 Intellisense 也在动态控制内部工作,这很好。
    【解决方案3】:

    终于经过两天的努力,我得到了解决格式化字段值的这种双向模型绑定问题的解决方案。只需去掉TextBox的属性文本后的单引号或双引号“”或''即可。它对我来说就像一个魅力!但是,如果您使用 TextBox to Date 的 TextMode 属性,请将其删除。原因,它不会显示来自服务器或数据库的值。如果您想使用日期选择器或日历,您可以简单地添加一个 AJAX CalendarExtender 控件来从日历中选择一个日期。

    <asp:TextBox ID="DateFieldTextBox" runat="server" Text=<%# Bind("DateField","{0:d}") %>/>
    

    【讨论】:

      【解决方案4】:

      很抱歉复活了这个帖子,但是您是否考虑将数据注释添加到您的模型类中?换句话说:

      using System.ComponentModel.DataAnnotations;
      
      [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
      public DateTime DateField { get; set; }
      

      这似乎对我有用,虽然我不知道你的情况的细节。

      【讨论】:

        猜你喜欢
        • 2013-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-19
        • 1970-01-01
        • 1970-01-01
        • 2017-09-21
        • 1970-01-01
        相关资源
        最近更新 更多