예제 #1
0
        public ActionResult Create([Bind(Include = "Id,Title,Created,Category,Description")] Portfolio portfolio, HttpPostedFileBase upload)
        {
            if (ModelState.IsValid)
            {
                if (upload != null && upload.ContentLength > 0)
                {
                    var photo = new File
                    {
                        FileName = System.IO.Path.GetFileName(upload.FileName),
                        FileType = FileType.Photo,
                        ContentType = upload.ContentType
                    };
                    using (var reader = new System.IO.BinaryReader(upload.InputStream))
                    {
                        photo.Content = reader.ReadBytes(upload.ContentLength);
                    }
                    portfolio.Files = new List<File> { photo };
                }
                db.Portfolio.Add(portfolio);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(portfolio);
        }
예제 #2
0
        public ActionResult Edit(int? id, HttpPostedFileBase upload)
        {
            if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); }
            var portfolio = db.Portfolio.Find(id);

            if (TryUpdateModel(portfolio, "", new string[] { "Title", "Description" }))
            {
                if (ModelState.IsValid)
                {

                    if (upload != null && upload.ContentLength > 0)
                    {
                        if (portfolio.Files.Any(f => f.FileType == FileType.Photo))
                        {
                            db.Files.Remove(portfolio.Files.First(f => f.FileType == FileType.Photo));
                        }
                        var photo = new File
                        {
                            FileName = System.IO.Path.GetFileName(upload.FileName),
                            FileType = FileType.Photo,
                            ContentType = upload.ContentType
                        };
                        using (var reader = new System.IO.BinaryReader(upload.InputStream))
                        {
                            photo.Content = reader.ReadBytes(upload.ContentLength);
                        }
                        portfolio.Files = new List<File> { photo };
                    }
                    db.Entry(portfolio).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }

            return View(portfolio);
        }