Kunde Hong
Kunde Hong

Reputation: 21

The MVC model's datetime format always can't be control

I'm trying to use DateTime with TextBoxFor to show n be edit ~ but one of my model's datetime format always can't be control~

View

@Html.TextBoxFor(model => model.ClassDate,"{0:yyyy-MM-dd}", new {  @readonly = "true",style = "width:135px",type="date"})
@Html.TextBoxFor(model => model.ClassDate,"{0:yyyy-MM-dd}", new {  @readonly = "true",style = "width:135px"})
@Html.TextBoxFor(model => model.TeacherInfo.BDay,"{0:yyyy-MM-dd}", new {  @readonly = "true",style = "width:135px",type="date"})
@Html.TextBoxFor(model => model.TeacherInfo.BDay,"{0:yyyy-MM-dd}", new {  @readonly = "true",style = "width:135px"})

Model(I have two table n using foreign key)

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> BDay { get; set; }

[DataType(DataType.DateTime)]
[DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> ClassDate { get; set; }

Result https://docs.google.com/file/d/0B53j6yH_R3ZeTjc2SmMxYS1OUkk/edit?usp=sharing

Upvotes: 0

Views: 3744

Answers (1)

Manan Vaghasiya
Manan Vaghasiya

Reputation: 922

Have you tried this way,

@Html.TextBoxFor( model=>model.ClassDate, new { @readonly = "true", style = "width:135px", type="date", @Value = Model.ClassDate.Day + "/" + Model.ClassDate.Month + " / " + Model.ClassDate.Year})

I had the same problem but this really works for me....

Upvotes: 1

Related Questions