gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:53:06 24 4
gpt4 key购买 nike

我有一个名为 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com