c# - Linq to SQL - 在 Html.TextBoxFor 中格式化 DateTime

标签 c# asp.net asp.net-mvc

我有一个名为 DExamination.dbml 的 .dbml Linq to SQL 类

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Examination")]
public partial class Examination : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _Id;

    private string _Title;

    private System.Nullable<System.DateTime> _StartDate;
}
...
    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_StartDate", DbType="DateTime")]
    public System.Nullable<System.DateTime> StartDate
    {
        get
        {
            return this._StartDate;
        }
        set
        {
            if ((this._StartDate != value))
            {
                this.OnStartDateChanging(value);
                this.SendPropertyChanging();
                this._StartDate = value;
                this.SendPropertyChanged("StartDate");
                this.OnStartDateChanged();
            }
        }
    }
...

在编辑中显示

<%: Html.TextBoxFor(model => model.Examination.StartDate)%>

如何将 StartDate 格式化为 "dd/MM/yyyy"

我试过在上面添加 DisplayFormat ...

[global::System.ComponentModel.DataAnnotations.DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_StartDate", DbType="DateTime")]
    public System.Nullable<System.DateTime> StartDate
    {
        get
        {
            return this._StartDate;
        }
        set
        {
            if ((this._StartDate != value))
            {
                this.OnStartDateChanging(value);
                this.SendPropertyChanging();
                this._StartDate = value;
                this.SendPropertyChanged("StartDate");
                this.OnStartDateChanged();
            }
        }
    }

但不工作

谁有解决办法?

最佳答案

您需要装饰您的模型:

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

并且该属性已经存在于您的代码中,只是顺序错误。

关于c# - Linq to SQL - 在 Html.TextBoxFor 中格式化 DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3245648/

相关文章:

asp.net-mvc - 使用多上下文应用程序自定义 ASPNET Identity 一对多关系

c# - 密码框验证

c# - ASP.NET 刷新 UpdatePanel OnClientClick

asp.net - 设置ASP.NET MVC开发环境

javascript - 如果第一个数字是0,我们怎么会无法进入。阅读下面的详细信息:

css - 母版页 CSS 使用母版页覆盖 Web 表单 CSS

javascript - 如何在脚本文件中使用 Razor View 中的模型?

c# - 如果推荐使用 xmlwriter,那么 xmltextwriter 的用途是什么

c# - c#文件读写

c# - RaisePropertyChanged() 没有更新单个按钮的值