public ActionResult Create(PostCreateViewModel pCVM, HttpPostedFileBase image) { if (ModelState.IsValid) { if (image != null) { pCVM.Post.ImageMimeType = image.ContentType; pCVM.Post.ImageData = new byte[image.ContentLength]; pCVM.Post.ImageFileName = image.FileName; image.InputStream.Read(pCVM.Post.ImageData, 0, image.ContentLength); } string[] tags = pCVM.NewTags.Split(' '); foreach (string tag in tags) { pCVM.Post.Tags.Add(new Tag() { TagValue = tag }); } db.Posts.Add(pCVM.Post); db.SaveChanges(); TempData["message"] = string.Format("Utworzono wpis: {0}", pCVM.Post.Title); return RedirectToAction("Index"); } return View(pCVM); }
public ActionResult Edit(PostCreateViewModel pCVM, HttpPostedFileBase image) { if (ModelState.IsValid) { if (image != null) { pCVM.Post.ImageMimeType = image.ContentType; pCVM.Post.ImageData = new byte[image.ContentLength]; pCVM.Post.ImageFileName = image.FileName; image.InputStream.Read(pCVM.Post.ImageData, 0, image.ContentLength); } else { pCVM.Post.ImageFileName = Request.Form["Post.ImageFileName"]; pCVM.Post.ImageMimeType = Request.Form["Post.ImageMimeType"]; if (pCVM.Post.ImageFileName != null) { pCVM.Post.ImageData = Convert.FromBase64String(Request.Form["Post.ImageData"]); } else pCVM.Post.ImageData = null; } List<Tag> oldTags = (from oTags in db.Tags where oTags.PostId == pCVM.Post.PostId select oTags).ToList(); if (oldTags.Count > 0) { foreach (var tag in oldTags) { db.Tags.Remove(tag); } } string[] tags = pCVM.NewTags.Split(' '); foreach (string tag in tags) { db.Tags.Add(new Tag() { TagValue = tag, PostId = pCVM.Post.PostId }); } db.Entry(pCVM.Post).State = EntityState.Modified; db.SaveChanges(); TempData["message"] = string.Format("Zmodyfikowano wpis: {0}", pCVM.Post.Title); return RedirectToAction("Index"); } return View(pCVM); }
public ActionResult Edit(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } PostCreateViewModel pCVM = new PostCreateViewModel(); pCVM.Post = db.Posts.Find(id); StringBuilder stringBuilder = new StringBuilder(); foreach (var tag in pCVM.Post.Tags) { stringBuilder.Append(tag.TagValue + " "); } pCVM.NewTags = stringBuilder.ToString(); if (pCVM.Post == null) { return HttpNotFound(); } else { pCVM.Post.UpdateTime = DateTime.Now; } return View(pCVM); }