2017-06-13 58 views
-1

我試圖將數據插入數據庫。我基本上試圖得到當前的日期,我也隱藏特定的領域(「更新日期),因爲我希望用戶看到。現在我想要的是每當我插入一些數據到數據庫創建日期應該自動插入。如何在用戶在數據庫中插入數據時插入當前日期和時間

控制器

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

savechangesmethod

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

相反'image.UploadDate = imageVM.uploadDate'的寫'image.UploadDate = DateTime.UtcNow;'???不太確定你在這裏問什麼。 – Igor

+0

@Igor我只是想抓住當前的日期插入上傳字段 – cedPound

+0

@Igor感謝您的建議,但我設法修復它 – cedPound

回答

0

我只需添加使用的Dat改變savechangesmethod eTime.Now方法。見下文。

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

}

相關問題