Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, March 3, 2012 3:30 PM
I have two dates (defined below) in an c# class in an mvc application. These dates are mapped to dates of datetime2 in a sql server database.
public virtual DateTime createdDate { get; set; }
public virtual DateTime modifiedDate { get; set; }
In the create method of the controller I set the dates as follows (user does not have access to set date):
vendor.createdDate = System.DateTime.Now;
vendor.modifiedDate = System.DateTime.Now;
This works fine.
However, if the record is modified, I only update the modified date in the edit method of the controller:
vendor.modifiedDate = System.DateTime.Now;
Works fine, however, the vendor.createdDate gets reset to 01/01/0001 and saved in the database on db.SaveChanges();
This overwrites the initial create date which obviously I do not want to happen. I am sure I have run across this before and solved it somehow but now my memory fails me.
This is not critical on this piece of the project because I do not expect these records to be updated very often, but still want it to work correctly because it will be critical in other parts of the project.
Any help greatly appreciated.
All replies (3)
Thursday, March 8, 2012 5:03 AM âś…Answered
Hi plunsford,
I found this thread, it may have you some tips to solve your problem.
Thanks.
Sunday, March 4, 2012 4:39 AM
Debug and check the value of vendor.createdDate for current vendor object that is being edited, when the execution enters EDIT method in your controller!
Sunday, March 4, 2012 3:06 PM
Thank you for your response. I have done this several times and the result is always the same. I just tried it again and the date retrieved from the database is 3/2/2012. (I manully change it back to the correct date in the database.) However, when I go into edit mode and save it, the createdDate is changed to 1/1/0001 and of course saved into the database when I save it. I checked and if I do not save it, i.e. return to the list view or details view, the createdDate does not change. Puzzling.
I think it has something to do with defining the property as System.DateTime, although I am not sure. Seems I had a problem similar to this some years ago in Microsoft Access but its been awhile and do not remember the details. Thanks again for support and if you have further suggestions they are appreciated. Certainly do not want to take up a great deal of your time.