Exemplo n.º 1
0
        public void EditItem(EditItemDTO item)
        {
            var itemDbModel = this.context.Items.SingleOrDefault(x => x.Id.ToString() == item.Id);

            foreach (var prop in item.GetType().GetProperties())
            {
                if (prop.GetValue(item, null) != null && prop.Name != "Id")
                {
                    if (itemDbModel.GetType().GetProperties().SingleOrDefault(x => x.Name == prop.Name) != null)
                    {
                        var propertyToModify = itemDbModel.GetType().GetProperties().SingleOrDefault(x => x.Name == prop.Name);
                        propertyToModify.SetValue(itemDbModel, prop.GetValue(item, null));
                    }
                }
            }

            this.context.SaveChanges();
        }
Exemplo n.º 2
0
        public IActionResult EditItem(EditItemViewModel model)
        {
            var item = new EditItemDTO();

            foreach (var prop in model.GetType().GetProperties())
            {
                if (prop.GetValue(model, null) != null)
                {
                    if (item.GetType().GetProperties().SingleOrDefault(x => x.Name == prop.Name) != null)
                    {
                        var propertyToModify = item.GetType().GetProperties().SingleOrDefault(x => x.Name == prop.Name);
                        propertyToModify.SetValue(item, prop.GetValue(model, null));
                    }
                }
            }

            this.itemService.EditItem(item);
            return(RedirectToAction("Index", "Home", new { message = "Termék sikeresen módosítva" }));
        }