public ManageWatchDetailViewModel PrepopulateEditValue(Watch watch, List <Movement> movement, List <WatchModel> watchModel) { var oldValue = db.Watches.Where(w => w.WatchID == watch.WatchID) .Select(w => new { w.WatchCode, w.Thumbnail }).FirstOrDefault(); var viewModel = new ManageWatchDetailViewModel { Movement = movement, WatchModel = watchModel, WatchId = watch.WatchID, WatchCode = oldValue.WatchCode, //giá trị cũ do giá trị mới bị trùng WatchDescription = watch.WatchDescription, Quantity = watch.Quantity, Price = watch.Price, MovementId = watch.MovementID, ModelId = watch.ModelID, WaterResistant = watch.WaterResistant, BandMaterial = watch.BandMaterial, CaseRadius = watch.CaseRadius, CaseMaterial = watch.CaseMaterial, PublishedTime = watch.PublishedTime, PublishedBy = watch.PublishedBy, Discount = watch.Discount, LedLight = watch.LEDLight, Guarantee = watch.Guarantee, Alarm = watch.Alarm, Thumbnail = oldValue.Thumbnail, //load lại thumbnail cũ Status = watch.Status }; return(viewModel); }
public ActionResult EditWatch([Bind(Include = "WatchId, WatchCode, WatchDescription, Quantity, Price, " + "MovementID, ModelID, BandMaterial, CaseMaterial, " + "CaseRadius, Discount, Guarantee, PublishedBy, PublishedTime")] Watch watch, HttpPostedFileBase thumbnail) { if (ModelState.IsValid) { watch.WaterResistant = Request["water"] == "yes"; watch.LEDLight = Request["led"] == "yes"; watch.Alarm = Request["alarm"] == "yes"; watch.Status = Request["status"] == "yes"; watch.WatchCode = watch.WatchCode.ToUpper(); bool duplicateCode = watchService.IsDuplicatedWatchCode(watch.WatchCode, watch.WatchID); bool validImage = true; if (thumbnail != null) { validImage = FileTypeDetector.IsImageFile(thumbnail); ; } if (duplicateCode || !validImage) { var movement = movementService.GetMovementList(); var watchModel = watchModelService.GetModelsList(); ManageWatchDetailViewModel viewModel = watchService.PrepopulateEditValue(watch, movement, watchModel); if (duplicateCode) { viewModel.DuplicateErrorMessage = "Watch with code '" + watch.WatchCode + "' already existed. Please choose another one"; } if (!validImage) { viewModel.InvalidImageFileMessage = "Invalid Thumbnail. Upload file is not image"; } return(View("~/Views/Admin/admin_manage_watch_detail.cshtml", viewModel)); } String oldValue = watchService.SerializeOldValue(watch.WatchID); //thumbnail.InputStream.Position = 0; if (watchService.UpdateWatchInfo(watch, thumbnail)) { //save old value to modification table String userId = Session.GetCurrentUserInfo("Username"); if (modificationService.CreateNewModificationHistory(watch.WatchID, oldValue, userId)) { TempData["SHOW_MODAL"] = @"<script>$('#successModal').modal();</script>"; return(RedirectToAction("ViewWatch", "Admin")); } } return(Content("Unexpected Error")); //change to 404 } return(RedirectToAction("NotFound", "Home")); }