gesirdekatwork
gesirdekatwork

Reputation: 87

MVC datatype.datetime has value but does not display it

input class="text-box single-line" data-val="true" data-val-date="The field Bill Date must be a date." data-val-required="Bill Date is required" id="BillDate" name="BillDate" type="date" value="22.02.2012"

here is html code shown in source on chrome. But there is no value in the textbox.

And here is my model for the "BillDate"

    [Required]
    [Display(Name = "Bill Date")]
    [DataType(DataType.Date, ErrorMessage = "Not a valid date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}")]
    public DateTime BillDate { set; get; }

and this is how i am trying to display it @Html.EditorFor(model => model.BillDate)

Can you help me please...

Upvotes: 2

Views: 1629

Answers (1)

eddievan
eddievan

Reputation: 88

I had this exact same problem. I ended up looking at the HTTP Post that was going back to the Controller and the format of the date in the POST was yyyy-MM-dd.

I changed my data annotation to this and it worked...

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]

Hope that helps!

Upvotes: 3

Related Questions