public ActionResult EditAsset(EditAssetViewModel vmEditAsset) { try { /* * if (ModelState.IsValid) * { * // transfer vm to dto * var dtoAssetTransaction = _unitOfWork.AssetTransactions.Get(vmEditAsset.Id); * if (dtoAssetTransaction != null) * { * dtoAssetTransaction.AssetId = DataTypeUtility.GetIntegerFromString(vmEditAsset.SelectedAssetId); * * // update db * _unitOfWork.CommitTrans(); * * // display view * TempData["SuccessMessage"] = "Asset Name updated"; * return RedirectToAction("Edit", "AccountTransaction", new { id = vmEditAsset.Id }); * } * } */ TempData["ErrorMessage"] = "Unable to edit record. Try again."; return(RedirectToAction("Index", "AccountTransaction")); } catch (Exception) { TempData["ErrorMessage"] = "Encountered Problem"; return(RedirectToAction("Index", "AccountTransaction")); } }
public ActionResult Edit(string id) { var asset = this.assetService.GetById(id); //Verify if asset is from user organisation if (!this.IsMegaAdmin()) { if (asset.Site.OrganisationId != this.userService.GetUserOrganisationId(this.User.Identity.GetUserId())) { return(Redirect("/Home/NotAuthorized")); } } var viewModel = new EditAssetViewModel { Brand = asset.Brand, Guarantee = asset.Guarantee, InventoryNumber = asset.InventoryNumber, ItemModel = asset.Model, Price = asset.Price.Value, Producer = asset.Producer, Type = asset.Type, SelectedCurrency = asset.Price.CurrencyId }; var user = this.userService.GetById(this.User.Identity.GetUserId()); if (!this.IsMegaAdmin()) { viewModel.Currency = this.currencyService.GetAll() .Where(x => x.OrganisationId == user.Site.OrganisationId).ToList() .ConvertAll(x => new CurrencyViewModel { Code = x.Code, Id = x.Id }).ToList(); } else { viewModel.Currency = this.currencyService.GetAll().ToList() .Where(x => x.OrganisationId == asset.Site.OrganisationId).ToList() .ConvertAll(x => new CurrencyViewModel { Code = x.Code, Id = x.Id }).ToList(); } viewModel.SiteName = asset.Site.Name; return(View(viewModel)); }
public ActionResult Edit(EditAssetViewModel viewModel) { if (!ModelState.IsValid) { return Edit(viewModel.Id); } var assetToUpdate = new Asset() { Id = viewModel.Id, Name = viewModel.Name }; _assetRepo.Update(assetToUpdate); _assetRepo.SaveChanges(); return Redirect(Url.Show(assetToUpdate)); }
public ActionResult Edit(int id) { var asset = _assetRepo.GetById(id); if (asset == null) { this.AddFlashError(Keys.NotFound<Asset>(), Messages.NotFound<Asset>()); return Redirect(Url.List<Asset>()); } var viewModel = new EditAssetViewModel() { Id = asset.Id, Name = asset.Name }; return View(viewModel); }
public ActionResult EditAsset(EditAssetViewModel vm) { var Success = false; var Message = ""; var nameValidation = Asset.Validator.ValidateName(vm.Name); if (nameValidation != null) { ModelState.AddModelError("Name", nameValidation); } var valueValidation = Asset.Validator.ValidateValue(vm.Value); if (valueValidation != null) { ModelState.AddModelError("Value", valueValidation); } var modelValidation = Asset.Validator.ValidateAssetModel(vm.SelectedAssetModelId); if (modelValidation != null) { ModelState.AddModelError("AssetModel", modelValidation); } var serialValidation = Asset.Validator.ValidateSerialKey(vm.SerialNumber); if (serialValidation != null) { ModelState.AddModelError("SerialNumber", serialValidation); } if (vm.Properties != null) { for (var i = 0; i < vm.Properties.Count; i++) { var p = vm.Properties[i]; var property = db.AssetModelProperties.Find(p.ModelPropertyId); var propertyValidation = Asset.Validator.ValidatePropertyValue(property, p.Value); if (propertyValidation != null) { ModelState.AddModelError($"Properties[{i}].Value", propertyValidation); } } } if (ModelState.IsValid) { var asset = db.Assets.Find(vm.Id); asset.Name = vm.Name; asset.SerialNumber = vm.SerialNumber; asset.Value = vm.Value; var oldProperties = asset.AssetProperties; var properties = new List <AssetProperty>(); asset.AssetModelId = vm.SelectedAssetModelId; if (vm.Properties != null) { foreach (var p in vm.Properties) { properties.Add(new AssetProperty { Asset = asset, AssetModelPropertyId = p.ModelPropertyId, Value = p.Value, }); } } asset.AssetProperties = properties; db.AssetProperties.RemoveRange(oldProperties); try { db.SaveChanges(); Success = true; Message = Asset.SAVE_SUCCESS; } catch (Exception) { Message = Asset.SAVE_FAIL; } return(Json(new { Success, Message })); } vm.AssetModels = db.AssetModelDropdown(); return(PartialView("_EditAsset", vm)); }
public ActionResult Edit(EditAssetViewModel model) { if (!ModelState.IsValid) { //Add all locations for choosing to viewmodel model.Locations = this.locationService.GetAll().ToList(). ConvertAll(x => new DropDownLocationViewModel { Code = x.Code, Location = x.Country + ", " + x.Town + ", " + x.Street + ", " + x.StreetNumber.Value }); var user = this.userService.GetById(this.User.Identity.GetUserId()); if (!this.IsMegaAdmin()) { model.Currency = this.currencyService.GetAll() .Where(x => x.OrganisationId == user.Site.OrganisationId).ToList() .ConvertAll(x => new CurrencyViewModel { Code = x.Code, Id = x.Id }).ToList(); } else { var asset = this.assetService.GetById(model.InventoryNumber); model.Currency = this.currencyService.GetAll().ToList() .Where(x => x.OrganisationId == asset.Site.OrganisationId).ToList() .ConvertAll(x => new CurrencyViewModel { Code = x.Code, Id = x.Id }).ToList(); } return(View(model)); } this.assetService.Update(new Asset { InventoryNumber = model.InventoryNumber, Producer = model.Producer, Brand = model.Brand, Model = model.ItemModel, Price = new Price { Value = model.Price, CurrencyId = model.SelectedCurrency }, Type = model.Type }); //Add a new row to asset' history this.assetHistoryService.AddHistoryRow(new HistoryRow { Content = "The asset information was updated.", Date = DateTime.Now }, model.InventoryNumber); return(Redirect("/AssetsActions/Asset/GetAll")); }