public ActionResult Create(AssetViewModel createAssetViewModel) { if (ModelState.IsValid) { Mapper.CreateMap<AssetViewModel, Asset>(); Mapper.CreateMap<AssetDetailViewModel, AssetDetail>(); Asset asset = Mapper.Map<AssetViewModel, Asset>(createAssetViewModel); AssetService.SaveAsset(asset); Alert(EnumHelper.Alerts.Success, HIResources.Strings.Change_Success); return RedirectToAction("Index"); } Alert(EnumHelper.Alerts.Error, HIResources.Strings.Change_Error); PopulateSelectLists(createAssetViewModel); return View(createAssetViewModel); }
/// <summary> /// Populate the View Model with the required Select Lists/Dropdowns /// </summary> /// <param name="assetViewModel"></param> /// <returns></returns> public void PopulateSelectLists(AssetViewModel assetViewModel) { IQueryable<Tenant> userTenants = new TenantUtility().GetUserTenants(User.Identity.Name); assetViewModel.TenantOrganisationSelectList = new SelectList(userTenants, "TenantId", "Name", assetViewModel.TenantId); assetViewModel.AssetMakeSelectList = new SelectList(db.Lookups.Where(l => l.Type.Description == EnumHelper.LookupTypes.Make.ToString()), "LookupId", "Description", assetViewModel.AssetMakeId); assetViewModel.CategorySelectList = new SelectList(db.Lookups.Where(l => l.Type.Description == EnumHelper.LookupTypes.Category.ToString()), "LookupId", "Description", assetViewModel.CategoryId); assetViewModel.WarrantyPeriodSelectList = new SelectList(db.Lookups.Where(l => l.Type.Description == EnumHelper.LookupTypes.WarrantyPeriod.ToString()), "LookupId", "Description", assetViewModel.WarrantyPeriodId); }
// GET: Assets/Create public ActionResult Create() { AssetViewModel createAssetViewModel = new AssetViewModel(); PopulateSelectLists(createAssetViewModel); return View(createAssetViewModel); }
public ActionResult Edit(AssetViewModel editAssetViewModel) { if (ModelState.IsValid) { Mapper.CreateMap<AssetViewModel, Asset>(); Asset asset = Mapper.DynamicMap<AssetViewModel, Asset>(editAssetViewModel); asset.PurchaseDate = editAssetViewModel.PurchaseDate; asset.AssetDetailId = asset.NetworkedAssetDetail.AssetDetailId; AssetService.SaveAsset(asset); Alert(EnumHelper.Alerts.Success, HIResources.Strings.Change_Success); return RedirectToAction("Index"); } var items = ModelState.ToList().Where(c => c.Value.Errors.Count() > 0); PopulateSelectLists(editAssetViewModel); Alert(EnumHelper.Alerts.Error, HIResources.Strings.Change_Error); return View(editAssetViewModel); }