tags:

views:

83

answers:

3

Hi,

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?

Thanks & regards

+1  A: 

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.

Mikael Svenson
Changed my answer.
Mikael Svenson
A: 

Have you tried:

<%: Html.TextBoxFor(model => model.Examination.StartDate.ToString("dd/MM/yyyy"))%>
Ben Robinson
It will not work, the html helper extension takes only property as argument
Gregoire
A: 

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) %>
Gregoire
thanks !!it works fine