Dinesh Kumar
Dinesh Kumar

Reputation: 1

Cant bind DateTime in mvc 5

I am having field in model:

[Display(Name = "Start Date")]
[Required(ErrorMessage = "Please select date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}")]
public DateTime Startdate { get; set; }

And in Controller:

ss.Startdate = sas.Startdate.Date;

Now the problem is if I debug and see the date is binding to Startdate and showing the date as per computer format. But after form is created then in list view it is not showing the startdate. It is showing 01/01/0001. How to get the startdate value into list view??

Upvotes: 0

Views: 875

Answers (1)

Ian Davidson
Ian Davidson

Reputation: 76

In your model snapshot you have the member, (named "Startdate"), which is of type DateTime, while your are using an attribute to format the data using type "date", which could be part of the confusion. Furthermore, a DateTime contains date of year as well as a time of day. While Date, is a stand alone date type.

Also 01/01/0001 is the lowest storable value of a DateTime object type. Meaning that could give you some feed back as to what is going on.

Your attributes added to the model->member Startdate should be enough to have a datepicker when entering that data for that field in a form (using bootstrap). BUT there is still no built in DateTime picker.

Additionally, for formatting DateTimes I found it very helpful to use a JS library called Moment.js data going to view as JSON format.

Other than that I cant quite articulate what this question is asking.

Upvotes: 1

Related Questions