public ActionResult Create(Post post) { // pega usuario logado var identity = User.Identity as ClaimsIdentity; var login = identity.Claims.FirstOrDefault(c => c.Type == "Login").Value; var userLoggedIn = db.Users.FirstOrDefault(u => u.Login == login); if (ModelState.IsValid) { post = new Post { Title = post.Title, Content = post.Content, Create_time = DateTime.Now, Update_time = DateTime.Now, Tag = post.Tag, ImageFile = post.ImageFile, CategoryId = post.CategoryId, UserId = userLoggedIn.UserId // pega usuario logado }; db.Posts.Add(post); db.SaveChanges(); if (post.ImageFile != null) { var pic = string.Empty; var folder = "~/Content/image/post"; var file = string.Format("{0}.jpg", post.PostId); var response = FilesHelper.UploadImage(post.ImageFile, folder, file); if (response) { pic = string.Format("{0}/{1}", folder, file); post.Image = pic; db.Entry(post).State = EntityState.Modified; db.SaveChanges(); } } TempData["MessagePanel"] = "Post realizado com sucesso"; return(RedirectToAction("List")); } ViewBag.CategoryId = new SelectList(CombosHelper.GetCategory(), "CategoryId", "Name"); //ViewBag.UserId = new SelectList(db.Users, "UserId", "Login", post.UserId); return(View(post)); }
public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Post post = db.Posts.Find(id); if (post == null) { return(HttpNotFound()); } ViewBag.CategoryId = new SelectList(CombosHelper.GetCategory(), "CategoryId", "Name", post.CategoryId); ViewBag.UserId = new SelectList(CombosHelper.GetUser(), "UserId", "Name", post.UserId); return(View(post)); }
public ActionResult Edit(Post post) { //// pega usuario logado //var identity = User.Identity as ClaimsIdentity; //var login = identity.Claims.FirstOrDefault(c => c.Type == "Login").Value; //var userLoggedIn = db.Users.FirstOrDefault(u => u.Login == login); if (ModelState.IsValid) { Post posts = db.Posts.Find(post.PostId); posts.Title = post.Title; posts.Content = post.Content; posts.Update_time = DateTime.Now; posts.Tag = post.Tag; posts.CategoryId = post.CategoryId; posts.UserId = post.UserId;// Pega usuario da ViewBag.UsuarioId opção de escolha //posts.UserId = userLoggedIn.UserId; // pega usuario logado posts.ImageFile = post.ImageFile; if (posts.ImageFile != null) { var pic = string.Empty; var folder = "~/Content/image/post"; var file = string.Format("{0}.jpg", posts.PostId); var response = FilesHelper.UploadImage(posts.ImageFile, folder, file); if (response) { pic = string.Format("{0}/{1}", folder, file); posts.Image = pic; } } db.Entry(posts).State = EntityState.Modified; db.SaveChanges(); TempData["MessagePanel"] = "Post editado com sucesso"; return(RedirectToAction("List")); } ViewBag.CategoryId = new SelectList(CombosHelper.GetCategory(), "CategoryId", "Name", post.CategoryId); ViewBag.UserId = new SelectList(CombosHelper.GetUser(), "UserId", "Login", post.UserId); return(View(post)); }
public ActionResult Create() { ViewBag.CategoryId = new SelectList(CombosHelper.GetCategory(), "CategoryId", "Name"); ViewBag.UserId = new SelectList(db.Users, "UserId", "Name"); return(View()); }