开发者

Linq to SQL - Format DateTime in Html.TextBoxFor

开发者 https://www.devze.com 2023-01-06 22:15 出处:网络
I\'m have .dbml Linq to SQL class named DExamination.dbml [global::System.Data.Linq.Mapping.TableAttribute(Name=\"dbo.Examination\")]

I'm have .dbml Linq to SQL class named DExamination.dbml

[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();
            }
        }
    }
...

Display in Edit

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

How to format StartDate like "dd/MM/yyyy"

I've tried add DisplayFormat above ...

[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开发者_运维问答();
            }
        }
    }

but not working

Anyone have solution?


You need to decorate your model:

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

And the attribute is already there in your code, just the wrong order.


You can create a directory EditorTemplates in your Shared view directory. Then add a control named DateTime.ascx with the following code:

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

Then you can call it with

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


Have you tried:

<%: Html.TextBoxFor(model => model.Examination.StartDate.ToString("dd/MM/yyyy"))%>
0

精彩评论

暂无评论...
验证码 换一张
取 消