2

HI i have an MVC application which have CreatedDate and ModifiedDate feilds, 1. CreatedDate is when user create the module(any entry) 2. ModifiedDate is when user Edit the module

I have following Model class

namespace MyForms.Models
{
    public class Master
    {
        public int ID { get; set; }
        public string ModuleName { get; set; }
        public int CreatedBy { get; set; }
        public DateTime ? CreatedDate { get; set; }
        public int ModifyBy { get; set; }
        public DateTime ModifyDate { get; set; }
        public Boolean IsActive { get; set; }
        public Boolean IsDeleted { get; set; }

         // public virtual ICollection<Master> MasterModules { get; set; }
    }
    public class MyFormDemoContext : DbContext
    {
        public DbSet<Master> MasterForms { get; set;}
    }
    }

Actions of Create and Edit

public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(Master master)
        {
            try
            {
                using (MyFormDemoContext context = new MyFormDemoContext()) 
                {
                    master.CreatedBy = 1;
                    master.CreatedDate = DateTime.Now;
                    var a = master.CreatedDate;
                    master.IsActive = true;
                    master.ModifyBy = 1;
                    master.ModifyDate = DateTime.Now;
                    master.IsDeleted = false;   
                    context.MasterForms.Add(master);
                    context.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Edit(int id)
        {
            using (MyFormDemoContext context = new MyFormDemoContext())
            {
                return View(context.MasterForms.Find(id));
            }
        }
        //
        // POST: /Home/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, Master valpara)
        {
            try
            {
                using (MyFormDemoContext context = new MyFormDemoContext())
                {
                    valpara.CreatedBy = 1;
                    valpara.CreatedDate = DateTime.Now;
                    valpara.IsActive = true;
                    valpara.ModifyBy = 1;
                    valpara.ModifyDate = DateTime.Now;
                    valpara.IsDeleted = false;
                    valpara.ModifyDate = DateTime.Now;
                    context.Entry(valpara).State = System.Data.EntityState.Modified;
                    context.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();

} }

1.currently when i create the module(entry) createdDate goes as current date 2. When i edit the module, modifiedDate and createdDate goes same

My expections

I want the createdDate Remains same when i Modify or edit the entry only modified date will be updated

Jot Dhaliwal
  • 1,470
  • 4
  • 26
  • 47

1 Answers1

4

When i edit the module, modifiedDate and createdDate goes same

Well, that's because in your Edit action you are specifically setting the CreatedDate, remove this line

valpara.CreatedDate = DateTime.Now

and only the ModifiedDate will be updated. However, a better approach would be to have your DB configured to set the date automatically (e.g. if you are using MSSQL set the default value to GetUtcDate()) and have EF pull that value instead of setting it client-side.

You need to set DatabaseGeneratedOption.Identity on that particular field which tells EF that the DB will generate the value.

FYI - you should really consider storing your dates as UTC rather than local i.e. use DateTime.UtcNow rather than DateTime.Now.


As well as the above, in your Edit you are actually re-creating a new entry each time. If you want to modify an existing record then you need to pull that record out of the DB first e.g.

using (MyFormDemoContext context = new MyFormDemoContext()) 
{
    var record = context.MasterForms.SingleOrDefault(x => x.ID == id);
    if (record != null)
    {
        record.ModifyBy = 1;
        record.ModifyDate = DateTime.UtcNow;
        context.SaveChanges();
    }
}
James
  • 80,725
  • 18
  • 167
  • 237
  • When i remove valpara.CreatedDate = DateTime.Now and edit the module, created date becomes null – Jot Dhaliwal Aug 26 '14 at 13:18
  • @JotDhaliwal what don't you understand? – James Aug 26 '14 at 13:24
  • Yes done , with DateTime.Now; UTC now is showing time like 1.26.. i am in india – Jot Dhaliwal Aug 26 '14 at 13:28
  • 1
    @JotDhaliwal yes but that's expected, UTC is the same time anywhere in the world. If you store as local then it means you can only work reliably with times that are in your local timezone. If your system is only ever going to work in India then you are safe to store as local. – James Aug 26 '14 at 13:43
  • hi James can you please guide me for the next step , i will great thankful to you for this – Jot Dhaliwal Aug 26 '14 at 14:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60013/discussion-between-jot-dhaliwal-and-james). – Jot Dhaliwal Aug 26 '14 at 14:38