Exemplo n.º 1
0
        public static House MapToHouse(OperateHouseViewModel operateHouseViewModel)
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <OperateHouseViewModel, House>();
                cfg.CreateMap <OwnerViewModel, Owner>();
            });
            var mapper = config.CreateMapper();
            var house  = mapper.Map <House>(operateHouseViewModel);

            return(house);
        }
Exemplo n.º 2
0
        public IActionResult Operate(OperateHouseViewModel operateHouseViewModel)
        {
            try
            {
                var house = ObjectAutoMapper.MapToHouse(operateHouseViewModel);

                ViewBag.Owners = new SelectList(_ownerManager.GetAll().Select(c => new OwnerViewModel
                {
                    FullName = c.FirstName + " " + c.LastName,
                    Id       = c.Id
                }), "Id", "FullName");

                if (ModelState.IsValid)
                {
                    if (house.Id != 0)
                    {
                        house.UpdatedDateTime = DateTime.Now;
                        house.UpdatedBy       = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                        _houseManager.Update(house);
                        return(RedirectToAction("List"));
                    }
                    else if (house.Id == 0)
                    {
                        house.CreatedDateTime = DateTime.Now;
                        house.CreatedBy       = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                        _houseManager.Create(house);
                        return(RedirectToAction("List"));
                    }
                    else
                    {
                        return(View("Operate"));
                    }
                }
                else
                {
                    return(View("Operate"));
                }
            }
            catch (Exception ex)
            {
                _logger.LogCritical(ex.Message);
                return(View("Error", new ErrorViewModel()
                {
                    Exception = ex, ActionName = "Operate", ControllerName = "House", RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier
                }));
            }
        }