Beispiel #1
0
        public ResponseModel Guardar(HttpPostedFileBase Foto)

        {
            var rm = new ResponseModel();

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

                    var eCategoria = db.Entry(this);
                    if (this.id_categoria > 0)
                    {
                        eCategoria.State = EntityState.Modified;

                        if (Foto != null)
                        {
                            String archivo = Path.GetFileName(Foto.FileName); //*Path.GetExtension(Foto.FileName);

                            //String archivo = DateTime.Now.ToString("yyyyMMddMMss") + Path.GetFileName(Foto.FileName);

                            Foto.SaveAs(HttpContext.Current.Server.MapPath("~/images/" + archivo));


                            this.img_categoria = archivo;
                        }
                        else
                        {
                            eCategoria.Property(x => x.img_categoria).IsModified = false;
                        }

                        db.SaveChanges();
                        rm.SetResponse(true);
                    }
                    else
                    {
                        eCategoria.State = EntityState.Added;

                        if (Foto != null)
                        {
                            String archivo = Path.GetFileName(Foto.FileName); //*Path.GetExtension(Foto.FileName);

                            //String archivo = DateTime.Now.ToString("yyyyMMddMMss") + Path.GetFileName(Foto.FileName);

                            Foto.SaveAs(HttpContext.Current.Server.MapPath("~/images/" + archivo));


                            this.img_categoria = archivo;
                        }
                        else
                        {
                            eCategoria.Property(x => x.img_categoria).IsModified = false;
                        }
                        db.SaveChanges();
                        rm.SetResponse(true);
                    }
                }
            }
            catch (DbEntityValidationException e)
            {
                throw;
            }
            catch (Exception)
            {
                throw;
            }
            return(rm);
        }