public Item Map(ItemEditViewModel source, Item destination) { if (source == null) { return null; } if (destination == null) { destination = new Item(); } destination.SerialNumber = source.SerialNumber; destination.Name = source.Name; destination.Type = source.Type; destination.Length = source.Length; destination.Height = source.Height; destination.Width = source.Width; destination.Weight = source.Weight; if (destination.States == null) { destination.States = new List<State>(); } return destination; }
public ItemEditViewModel Map(Item source, ItemEditViewModel destination) { if (source == null) { return null; } if (destination == null) { destination = new ItemEditViewModel(); } destination.Id = source.Id; destination.Name = source.Name; destination.SerialNumber = source.SerialNumber; destination.Type = source.Type; destination.Length = source.Length; destination.Height = source.Height; destination.Width = source.Width; destination.Weight = source.Weight; var state = source.States.FirstOrDefault(); destination.States = state == null ? StateEnum.Registered : (StateEnum)state.Id; return destination; }
public virtual ActionResult Edit(ItemEditViewModel model) { if (model == null || !ModelState.IsValid) { return View(model); } try { var originalItem = itemService.Get(model.Id); if (originalItem == null) { return RedirectToAction(MVC.Item.List()); } var stateId = originalItem.States.FirstOrDefault() == null ? 0 : originalItem.States.FirstOrDefault().Id; var updated = ViewModelToEntityMapper.Mapper.Map(model, originalItem); var states = itemService.GetAllStates(); updated.States = new List<State>(); if (states != null && states.Any()) { updated.States.Add(states.Find(x => x.Id == (int)model.States)); } var savedItem = itemService.Save(updated); if (savedItem != null) { if (savedItem.States.FirstOrDefault().Id != stateId) { itemLogService.Save(savedItem); } ModelState.Clear(); var viewModel = EntityToViewModelMapper.Mapper.Map(savedItem, new ItemEditViewModel()); viewModel.Message = new MessageViewModel { IsError = false, Message = "Įrašas atnaujintas" }; return View(viewModel); } } catch (Exception ex) { model.Message = new MessageViewModel { IsError = true, Message = "Išsaugant įvyko klaida, bandykite dar kartą" }; return View(model); } return RedirectToAction(MVC.Item.List()); }