Exemple #1
0
        public ActionResult CrearEditarFamilia(int id, FamiliaViewModel viewModel)
        {
            try
            {
                var familia = new TbPrFamilia();
                var edita   = false;

                var existeFamilia = service.GetFamiliaByDescripcion(viewModel.Descripcion);

                if (id == 0)
                {
                    if (existeFamilia != null)
                    {
                        return(Json(new { success = false }));
                    }

                    viewModel.IdUsuario = int.Parse(User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value);
                    familia             = map.Create(viewModel);
                }
                else
                {
                    if (existeFamilia != null)
                    {
                        if (existeFamilia.Id != id)
                        {
                            return(Json(new { success = false }));
                        }
                    }

                    familia = map.Update(id, viewModel);
                    edita   = true;
                }

                familia.IdFamiliaNavigation = null;
                foreach (var item in familia.InverseIdFamiliaNavigation)
                {
                    item.IdFamiliaNavigation = null;
                }


                return(Json(new { success = true, familia = familia, edita = edita }));
            }
            catch (Exception ex)
            {
                AltivaLog.Log.Insertar(ex.ToString(), "Error");
                return(Json(new { data = false }));;
            }
        }