예제 #1
0
        public ActionResult Create([Bind(Include = "Id,IdUser,ProductName,Description,Status,RegisterDate")] Products products, HttpPostedFileBase upload)
        {
            if (ModelState.IsValid)
            {
                if (upload != null && upload.ContentLength > 0)
                {
                    var avatar = new File
                    {
                        FileName = System.IO.Path.GetFileName(upload.FileName),
                        FileType = FileType.Imagen,
                        ContentType = upload.ContentType
                    };
                    using (var reader = new System.IO.BinaryReader(upload.InputStream))
                    {
                        avatar.Content = reader.ReadBytes(upload.ContentLength);
                    }
                    products.Files = new List<File> { avatar };
                }
                products.IdUser = User.Identity.GetUserId();
                products.RegisterDate = @DateTime.Now;
                products.Status = "Activo";
                db.Products.Add(products);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(products);
        }
예제 #2
0
        public ActionResult Edit([Bind(Include = "Id,IdUser,ProductName,Description,Status,RegisterDate")] Products products, HttpPostedFileBase upload)
        {
            //var productsUpdate = db.Products.Find(products.Id);
            if (ModelState.IsValid)
            {

                if (upload != null && upload.ContentLength > 0)
                {
                    if (products.Files.Any(f => f.FileType == FileType.Imagen))
                    {
                        db.Files.Remove(products.Files.First(f => f.FileType == FileType.Imagen));
                    }
                    var avatar = new File
                    {

                        FileName = System.IO.Path.GetFileName(upload.FileName),
                        FileType = FileType.Imagen,
                        ContentType = upload.ContentType
                    };
                    using (var reader = new System.IO.BinaryReader(upload.InputStream))
                    {
                        avatar.Content = reader.ReadBytes(upload.ContentLength);
                    }
                    products.Files = new List<File> { avatar };
                }
                products.IdUser = User.Identity.GetUserId();
                db.Entry(products).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(products);
        }