C# Linq to SQL-在Html.TextBoxFor中格式化日期时间

C# Linq to SQL-在Html.TextBoxFor中格式化日期时间,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我有一个名为DExamination.dbml的.dbml Linq到SQL类 [global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Examination")] public partial class Examination : INotifyPropertyChanging, INotifyPropertyChanged { private static PropertyChangingEventArgs empty

我有一个名为DExamination.dbml的.dbml Linq到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();
            }
        }
    }
...
[global::System.Data.Linq.Mapping.TableAttribute(Name=“dbo.examing”)]
公开部分类考试:INotifyPropertyChanged,INotifyPropertyChanged
{
私有静态属性ChangingEventArgs emptyChangingEventArgs=新属性ChangingEventArgs(String.Empty);
私人内部Id;
私有字符串\u标题;
私有系统。可为空的_StartDate;
}
...
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage=“\u StartDate”,DbType=“DateTime”)]
公共系统。可为空的起始日期
{
得到
{
把这个还给我。_StartDate;
}
设置
{
如果((此._StartDate!=值))
{
此.onStartDateChanged(值);
此参数为.SendPropertyChanging();
这是。_StartDate=值;
此。SendPropertyChanged(“StartDate”);
this.OnStartDateChanged();
}
}
}
...
以编辑方式显示

<%: Html.TextBoxFor(model => model.Examination.StartDate)%>
model.examition.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();
            }
        }
    }
[global::System.ComponentModel.DataAnnotations.DisplayFormat(ApplyFormatInEditMode=true,DataFormatString=“{0:MM/dd/yyyyy}”)]
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage=“\u StartDate”,DbType=“DateTime”)]
公共系统。可为空的起始日期
{
得到
{
把这个还给我。_StartDate;
}
设置
{
如果((此._StartDate!=值))
{
此.onStartDateChanged(值);
此参数为.SendPropertyChanging();
这是。_StartDate=值;
此。SendPropertyChanged(“StartDate”);
this.OnStartDateChanged();
}
}
}
但不起作用


有人有解决方案吗?

您需要装饰您的模型:

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

属性已经存在于代码中,只是顺序错误。

您需要装饰您的模型:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime StartDate{ get; set; }
属性已经存在于代码中,只是顺序错误。

您尝试过:

<%: Html.TextBoxFor(model => model.Examination.StartDate.ToString("dd/MM/yyyy"))%>
model.examition.StartDate.ToString(“dd/MM/yyyyy”))%
您是否尝试过:

<%: Html.TextBoxFor(model => model.Examination.StartDate.ToString("dd/MM/yyyy"))%>
model.examition.StartDate.ToString(“dd/MM/yyyyy”))%

您可以在
共享
查看目录中创建目录
编辑器或模板。
然后使用以下代码添加名为
DateTime.ascx
的控件:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %>
<%:Html.TextBox(string.Empty,Model.ToString("dd/MM/yyyy")) %>

那你就可以用

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

您可以在
共享
查看目录中创建目录
编辑器或模板。
然后使用以下代码添加名为
DateTime.ascx
的控件:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %>
<%:Html.TextBox(string.Empty,Model.ToString("dd/MM/yyyy")) %>

那你就可以用

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

它将不起作用,html helper扩展只接受属性作为参数它将不起作用,html helper扩展只接受属性作为参数这将应用于您选择使用
EditorFor
的每个DateTime模型属性,我从海报的注释中获取它,这正是他所寻找的。但是,如果要有条件地将自定义编辑器/显示样板应用于一个或多个模型特性,您可以使用'EditorFor/DisplayFor'覆盖,该覆盖以templateName作为参数。这将应用于您选择使用
EditorFor
的每个DateTime模型属性,我从海报的注释中获取了该属性,这正是他要找的。但是,如果希望有条件地将自定义编辑器/显示模板应用于一个或多个模型属性,则可以使用以templateName为参数的“EditorFor/DisplayFor”替代。