Asp.net mvc 4 用户在数据库中插入数据时如何插入当前日期和时间

Asp.net mvc 4 用户在数据库中插入数据时如何插入当前日期和时间,asp.net-mvc-4,Asp.net Mvc 4,我正在尝试将数据插入数据库。我基本上是在尝试获取当前日期,我还隐藏了那个特定的字段(“更新日期”),因为我确实希望用户看到。现在我想要的是每当我向数据库插入一些数据时,创建的日期都应该自动插入 控制器 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(StoreImageCreateVM StoreImageVM) { if (ModelState.IsValid)

我正在尝试将数据插入数据库。我基本上是在尝试获取当前日期,我还隐藏了那个特定的字段(“更新日期”),因为我确实希望用户看到。现在我想要的是每当我向数据库插入一些数据时,创建的日期都应该自动插入

控制器

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(StoreImageCreateVM StoreImageVM)
    {
        if (ModelState.IsValid)
        {
            try
            {
                StoreImageLogic.Insert(StoreImageVM);
            }
            catch (Exception e) 
            {
                TempData["Failure"] = "Creation Failed. Image too large.";
                return View(StoreImageVM);
            }
            TempData["Success"] = "Creation Successful";
            return RedirectToAction("Index", new { id = StoreImageVM.StoreID });
        }
  return View(StoreImageVM);
    }
保存更改方法

 public bool Insert(StoreImageCreateVM imageVM)
    {
        StoreImage image = new StoreImage();

        image.StoreID = imageVM.StoreID;
        image.ImageDescription = imageVM.Description;
        image.UploadDate = imageVM.uploadDate;



        //Upload the file
        string path = AppDomain.CurrentDomain.BaseDirectory + @"Content\Uploads";            
        string filename = imageVM.File.FileName;
        string fullPath = Path.Combine(path, filename);

        imageVM.File.SaveAs(fullPath);

        //Set imageURL
        string serverFilePath = @"\Content\Uploads\";
        image.FullFilePath = serverFilePath + filename;
        image.Active = true;

        return base.Insert(image).StoreImageID != 0;
    }
}

我只是使用DateTime.Now方法添加以更改savechangesmethod。请参见下文

        public bool Insert(StoreImageCreateVM imageVM)
    {
        StoreImage image = new StoreImage();

        image.StoreID = imageVM.StoreID;
        image.ImageDescription = imageVM.Description;
        image.UploadDate = DateTime.Now;



        //Upload the file
        string path = AppDomain.CurrentDomain.BaseDirectory + @"Content\Uploads";            
        string filename = imageVM.File.FileName;
        string fullPath = Path.Combine(path, filename);

        imageVM.File.SaveAs(fullPath);

        //Set imageURL
        string serverFilePath = @"\Content\Uploads\";
        image.FullFilePath = serverFilePath + filename;
        image.Active = true;

        return base.Insert(image).StoreImageID != 0;
    }
}

}

而不是
image.UploadDate=imageVM.UploadDate
write
image.UploadDate=DateTime.UtcNow;
??我不太确定你在这里问什么。@我只是想抓取当前日期插入上传field@Igor谢谢你的建议,但我设法解决了