Example #1
0
        public ActionResult Create(empresa Newempresa, HttpPostedFileBase img)
        {
            using (db_globalesEntities2 db = new db_globalesEntities2())
            {
                //upload folder exist
                if (!Directory.Exists(Server.MapPath("~/Images")))
                {
                    Directory.CreateDirectory(Server.MapPath("~/Images"));
                }
                //save image on local storage
                var path = Path.Combine(Server.MapPath("~/Images"), img.FileName);
                img.SaveAs(path);
                Newempresa.imagen      = img.FileName;
                Newempresa.propietario = Session["UserID"].ToString();



                //empresa exists
                var empresa = db.empresa.Where(x => x.propietario == Newempresa.propietario).FirstOrDefault();
                if (empresa == null)
                {
                    try
                    {
                        db.empresa.Add(Newempresa);
                        db.SaveChanges();
                    }
                    catch (DbEntityValidationException dbEx)
                    {
                        foreach (var validationErrors in dbEx.EntityValidationErrors)
                        {
                            foreach (var validationError in validationErrors.ValidationErrors)
                            {
                                System.Console.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                            }
                        }
                    }
                }
                else
                {
                    empresa.nombre      = Newempresa.nombre;
                    empresa.provincia   = Newempresa.provincia;
                    empresa.descripcion = Newempresa.descripcion;
                    empresa.imagen      = Newempresa.imagen;
                    UpdateModel(empresa);
                    db.SaveChanges();
                }
            }


            return(View("Index", Newempresa));
        }
Example #2
0
        public ActionResult CreateEvent(eventos Newevent, HttpPostedFileBase img)
        {
            using (db_globalesEntities2 db = new db_globalesEntities2())
            {
                //upload folder exist
                if (!Directory.Exists(Server.MapPath("~/Images/Eventos")))
                {
                    Directory.CreateDirectory(Server.MapPath("~/Images/Eventos"));
                }
                //save image on local storage
                var path = Path.Combine(Server.MapPath("~/Images/Eventos"), img.FileName);
                img.SaveAs(path);
                Newevent.imagen      = img.FileName;
                Newevent.propietario = Session["UserID"].ToString();



                //empresa exists
                var empresa = db.eventos.Where(x => x.id == Newevent.id).FirstOrDefault();
                if (empresa == null)
                {
                    db.eventos.Add(Newevent);
                    db.SaveChanges();
                }
            }


            return(RedirectToAction("Index", "Home"));
        }