public ActionResult Create(CreatePostViewModel _post) { string tags = _post.Tags; _post.Tags = null; var post = Mapper.Map <CreatePostViewModel, Post>(_post); post.AuthorId = ((Student)Session["User"]).Id; db.Posts.Add(post); db.SaveChanges(); post = db.Entry(post).Entity; foreach (var tag in tags.Split(' ').Distinct()) { var buf = db.Tags.Where(a => a.Name == tag).SingleOrDefault(); if (buf == null) { db.Tags.Add(new Tags() { Name = tag }); db.SaveChanges(); buf = db.Tags.Where(a => a.Name == tag).SingleOrDefault(); } post.Tags.Add(buf); } db.SaveChanges(); //foreach (var tag in tags.Split(' ').Distinct()) //{ // var buf = db.Tags.Where(a => a.Name == tag).SingleOrDefault(); // db.TagsPosts.Add((buf == null) // ? new TagsPosts() { // Tags = new Tags() { Name = tag }, // IdPost = post.Id, // Post = post // } // : new TagsPosts() { // IdTag = buf.Id, // Tags = buf, // IdPost = post.Id, // Post = post // }); // db.SaveChanges(); //} return(RedirectToAction("Index")); }
public ActionResult Edit(EditCommentViewModel _comment) { var comment = Mapper.Map <EditCommentViewModel, Comment>(_comment); db.Comments.Add(comment); db.Entry(comment).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Details", "Posts", new { id = comment.PostId })); }
public ActionResult Edit(EditPostViewModel postView) { var post = Mapper.Map <EditPostViewModel, Post>(postView); db.Posts.Add(post); db.Entry(post).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); }