Adam
Adam

Reputation: 344

MVC 5 DateTime POST to Controller showing as 1/1/0001

I am having some issues with editing a model from the scaffold created view and controller created using a database first model when the model has a DateTime field. Creating a new record works fine. When trying to edit the newly created record will bring in the proper date and time in the generated edit page. The problem occurs when I try to save the record back to the database. The model on the controller shows the created while debugging shows the DateTime field as 1/1/0001 12:00:00. I am not sure what would be causing this issue as I am using the standard generated code for the Model View and Controller. In the autos window while debugging I see under ValueProvider that the Key has the correct ValidatedResult as well.

Here is the model code as generated from the entity framework

    public partial class Feature
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Feature()
    {
        this.FeatureActions = new HashSet<FeatureAction>();
    }

    public System.Guid Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public bool InActive { get; set; }
    public bool System { get; set; }
    public byte[] Version { get; set; }
    public System.DateTime CreatedDate { get; set; }
    public System.DateTime UpdatedDate { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<FeatureAction> FeatureActions { get; set; }
}

and here is the method on the controller

  [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "Id,Name,Description,InActive,System,Version,CreatedDate,UpdatedDate")] Feature feature)
    {
        if (ModelState.IsValid)
        {
            feature.UpdatedDate = DateTime.Now;
            db.Entry(feature).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(feature);
    }

Any thoughts or suggestions are appreciated!

Upvotes: 2

Views: 4591

Answers (3)

Mohamed Elamin
Mohamed Elamin

Reputation: 375

HTML5 input type="date" shows date as yyyy-MM-dd. so first you need to change your date format by:

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

Second, the DateTime that not accept null will take the initial value DateTime.MinValue which return Date = {1/1/0001 12:00:00 AM} so make your datetime accept null by public DateTime? CreatedDate { get; set; } and if your datetime in the database not accept null, On your model class decorate that property with [Required] attribute

[Required]
[Display(Name = "Created Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? CreatedDate { get; set; }

Upvotes: 0

Grizzly
Grizzly

Reputation: 5943

Usually if the DateTime valued is display like 1/1/0001 12:00:00 then the binding is wrong (maybe a misspelled name).. but with the way your properties are named, it looks like the user shouldn't be able to edit those values, which is what I see in your controller with feature.UpdatedDate = DateTime.Now

In your model try:

[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy, hh.mm tt}", ApplyFormatInEditMode = true)]
public System.DateTime CreatedDate { get; set; }

[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy, hh.mm tt}", ApplyFormatInEditMode = true)]
public System.DateTime UpdatedDate { get; set; }

Upvotes: 0

Rafael Ribeiro
Rafael Ribeiro

Reputation: 188

If you are using a third-party plugin like datetimepicker, you have to see what the datetime format for this plugin is on.

If I understood the problem, these plugins has a default format than whether the format received is not compatible, It put the default.

Hope this give you some help. :)

Upvotes: 1

Related Questions