Handling Created and Modified Date in MVC

HI I have an MVC application in which the CreateDate and ModifiedDate functions are created, 1. CreatedDate is when the user creates a module (any record) 2. ModifiedDate is when the user edits the module

I have a 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;}
    }
    }

      

Create and edit actions

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 a module (record) createdDate goes as the current date 2. When I edit the module, modifiedDate and createdDate are the same

My expectations

I want createdDate to stay the same when I change or edit the entry, only the updated date will be updated

0


source to share


1 answer


When I edit the module the modifiedDate and createdDate become the same

Ok, because in your action Edit

you are specifically setting CreatedDate

, remove this line

valpara.CreatedDate = DateTime.Now

      

and will only be updated ModifiedDate

. However, the best approach is to have your DB set up to set the date automatically (for example, if you are using MSSQL, set the default to GetUtcDate () ) and EF pull that value instead of setting it on the client side.

You need to set DatabaseGeneratedOption.Identity

on that particular field which tells EF that the DB will generate a value.



FYI - you should really consider storing your dates as UTC and not local, then use DateTime.UtcNow

instead DateTime.Now

.


As above, in yours, Edit

you actually create a new record every time. If you want to modify an existing entry, you need to pull that entry from the DB first, for example.

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();
    }
}

      

+2


source







All Articles