处理创建和MVC修改日期 [英] Handling Created and Modified Date in MVC

查看:110
本文介绍了处理创建和MVC修改日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有哪些有CreatedDate和ModifiedDate精密组件MVC应用程序,
1. CreatedDate是当用户创建模块(的任何条目)
2. ModifiedDate是当用户编辑模块

我有以下Model类

 命名空间MyForms.Models
{
    公共类硕士
    {
        公众诠释ID {搞定;组; }
        公共字符串模块名{获得;组; }
        公众诠释CreatedBy {搞定;组; }
        公众的DateTime? CreatedDate {搞定;组; }
        公众诠释ModifyBy {搞定;组; }
        公众的DateTime ModifyDate {搞定;组; }
        公共布尔IsActive {搞定;组; }
        市民请将isDeleted布尔{搞定;组; }         //公共虚拟的ICollection<硕士> MasterModules {搞定;组; }
    }
    公共类MyFormDemoContext:的DbContext
    {
        公共DbSet<硕士> MasterForms {搞定;组;}
    }
    }

的操作创建和编辑

 公众的ActionResult的Create()
        {
            返回查看();
        }
        [HttpPost]
        公众的ActionResult创建(法师)
        {
            尝试
            {
                使用(MyFormDemoContext上下文=新MyFormDemoContext())
                {
                    master.CreatedBy = 1;
                    master.CreatedDate = DateTime.Now;
                    VAR一个= master.CreatedDate;
                    master.IsActive = TRUE;
                    master.ModifyBy = 1;
                    master.ModifyDate = DateTime.Now;
                    master.IsDeleted = FALSE;
                    context.MasterForms.Add(主);
                    context.SaveChanges();
                }
                返回RedirectToAction(「指数」);
            }
            抓住
            {
                返回查看();
            }
        }
        公众的ActionResult编辑(INT ID)
        {
            使用(MyFormDemoContext上下文=新MyFormDemoContext())
            {
                返回查看(context.MasterForms.Find(ID));
            }
        }
        //
        // POST:/主页/编辑/ 5
        [HttpPost]
        公众的ActionResult编辑(INT ID,硕士valpara)
        {
            尝试
            {
                使用(MyFormDemoContext上下文=新MyFormDemoContext())
                {
                    valpara.CreatedBy = 1;
                    valpara.CreatedDate = DateTime.Now;
                    valpara.IsActive =真;
                    valpara.ModifyBy = 1;
                    valpara.ModifyDate = DateTime.Now;
                    valpara.IsDeleted = FALSE;
                    valpara.ModifyDate = DateTime.Now;
                    context.Entry(valpara).STATE = System.Data.EntityState.Modified;
                    context.SaveChanges();
                }
                返回RedirectToAction(「指数」);
            }
            抓住
            {
                返回查看();

}}

1.currently当我创建模块(输入)createdDate去为当前日期
2.当我编辑的模块,modifiedDate和createdDate去同一个

我的expections

我想,当我修改或编辑条目只修改日期将被更新createdDate仍然相同


解决方案

  

当我编辑模块,modifiedDate和createdDate去同一个


嗯,那是因为在你的修改操作你特别设置 CreatedDate ,删除此行

  valpara.CreatedDate = DateTime.Now

只有 ModifiedDate 将被更新。然而,更好的办法是有你的数据库配置为自动设定日期(例如,如果您使用的是MSSQL设置的默认值的 GETUTCDATE()),并有EF扳指的价值,而不是将它设置客户端的。

您需要设置 DatabaseGeneratedOption.Identity 上告诉EF,该数据库将产生价值的特定领域。

仅供参考 - 你应该的真正的考虑将您的时间存在UTC而不是本地使用,即 DateTime.UtcNow ,而不是 DateTime.Now


除了上面的,在你的修改你是真正的重新创建的新条目各一次。如果你想的修改的现有记录,那么你需要拉该记录了DB的第一个例如

 使用(MyFormDemoContext上下文=新MyFormDemoContext())
{
    VAR纪录= context.MasterForms.SingleOrDefault(X => x.ID == ID);
    如果(纪录!= NULL)
    {
        record.ModifyBy = 1;
        record.ModifyDate = DateTime.UtcNow;
        context.SaveChanges();
    }
}

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

解决方案

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

这篇关于处理创建和MVC修改日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆