处理MVC中创建和修改的日期

时间:2014-08-26 13:09:10

标签: asp.net asp.net-mvc-3

我有一个MVC应用程序,它有CreatedDate和ModifiedDate字段, 1. CreatedDate是用户创建模块时(任何条目) 2. ModifiedDate是用户编辑模块时的

我有以下Model类

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

创建和编辑的操作

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.当我创建模块(条目)时,createdDate作为当前日期 2.当我编辑模块时,modifiedDate和createdDate相同

我的预算

我希望在修改或编辑条目时,createdDate保持相同,仅修改日期

1 个答案:

答案 0 :(得分:3)

  

当我编辑模块时,modifiedDate和createdDate相同

嗯,那是因为在您的Edit行动中,您专门设置了CreatedDate,请删除此行

valpara.CreatedDate = DateTime.Now

只会更新ModifiedDate。但是,更好的方法是将数据库配置为自动设置日期(例如,如果您使用MSSQL将默认值设置为GetUtcDate())并让EF拉取该值而不是将其设置为客户端。 / p>

您需要在该特定字段上设置DatabaseGeneratedOption.Identity,告知EF该DB将生成该值。

仅供参考 - 您应该确实考虑将日期存储为UTC而不是本地,即使用DateTime.UtcNow而不是DateTime.Now


除此之外,在您的Edit中,您实际上每次都在重新创建一个新条目。如果您想修改现有记录,那么您需要先将该记录从数据库中提取出来,例如。

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