Exemple #1
0
        public ResponseModel GuardarPerfil(HttpPostedFileBase Foto)
        {
            var rm = new ResponseModel();

            try
            {
                using (var db = new Model_Sistema())
                {
                    db.Configuration.ValidateOnSaveEnabled = false;

                    var usu = db.Entry(this);
                    usu.State = EntityState.Modified;

                    if (Foto != null)
                    {
                        const int size            = 1024 * 1024 * 5;
                        var       filtroextension = new[] { ".jpg", ".jpeg", ".png", ".gif" };
                        var       extensiones     = Path.GetExtension(Foto.FileName);

                        if (filtroextension.Contains(extensiones) && (Foto.ContentLength <= size))
                        {
                            avatar = usuario_id + "_avatar" + extensiones;
                            Foto.SaveAs(HttpContext.Current.Server.MapPath("~/Uploads/" + avatar));
                        }
                        else
                        {
                            usu.Property(x => x.avatar).IsModified = false;
                        }

                        if (this.usuario_id == 0)
                        {
                            usu.Property(x => x.usuario_id).IsModified = false;
                        }
                        if (this.persona_id == 0)
                        {
                            usu.Property(x => x.persona_id).IsModified = false;
                        }
                        if (this.usuario1 == null)
                        {
                            usu.Property(x => x.usuario1).IsModified = false;
                        }
                        if (this.nivel == null)
                        {
                            usu.Property(x => x.nivel).IsModified = false;
                        }
                        if (this.estado == null)
                        {
                            usu.Property(x => x.estado).IsModified = false;
                        }
                        if (this.clave == null)
                        {
                            usu.Property(x => x.clave).IsModified = false;
                        }

                        db.SaveChanges();
                        rm.SetResponse(true);
                    }
                }
            }
            catch (DbEntityValidationException)
            {
                throw;
            }

            return(rm);
        }