Exemplo n.º 1
0
        public ActionResult Create([Bind(Include = "ID,Title,Body,Date,Type,ContentType")] Tutorial tutorial, HttpPostedFileBase upload)
        {
            string userid = User.Identity.GetUserId();
            var currentuser = db.Users.SingleOrDefault(u => u.Id == userid);

            if (ModelState.IsValid)
            {

                if (upload != null && upload.ContentLength > 0)
                {
                    var pic = new FileMain
                    {
                        FileName = System.IO.Path.GetFileName(upload.FileName),
                        FileType = FileType.Pic,
                        ContentType = upload.ContentType
                    };
                    using (var reader = new System.IO.BinaryReader(upload.InputStream))
                    {
                        pic.Content = reader.ReadBytes(upload.ContentLength);
                    }
                    tutorial.FileMains = new List<FileMain> { pic };
                }

                tutorial.User = currentuser;
                tutorial.Date = DateTime.Now;

                db.Tutorials.Add(tutorial);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(tutorial);
        }
Exemplo n.º 2
0
        public ActionResult Edit(Tutorial model, HttpPostedFileBase upload, int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Tutorial tutorial = db.Tutorials.Find(id);
            if (tutorial == null)
            {
                return HttpNotFound();
            }

            if (ModelState.IsValid)
            {
                //int tutorialid = tutorial.ID;
                //var tutorialToUpdate = db.Tutorials.SingleOrDefault(u => u.ID == tutorialid);

                if (upload != null && upload.ContentLength > 0)
                {
                    if (tutorial.FileMains.Any(f => f.FileType == FileType.Pic))
                    {
                        db.FileMains.Remove(tutorial.FileMains.First(f => f.FileType == FileType.Pic));
                    }
                    var pic = new FileMain
                    {
                        FileName = System.IO.Path.GetFileName(upload.FileName),
                        FileType = FileType.Pic,
                        ContentType = upload.ContentType
                    };
                    using (var reader = new System.IO.BinaryReader(upload.InputStream))
                    {
                        pic.Content = reader.ReadBytes(upload.ContentLength);
                    }
                    tutorial.FileMains = new List<FileMain> { pic };
                }

                tutorial.Title = model.Title;
                tutorial.Body = model.Body;
                tutorial.Type = model.Type;
                tutorial.ContentType = model.ContentType;
                tutorial.Date = DateTime.Now;

                db.Entry(tutorial).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(tutorial);
        }