Created
May 18, 2015 13:42
-
-
Save codebeaulieu/d9a74b4414fdb18021ca to your computer and use it in GitHub Desktop.
PostController.cs | programmers.stackexchange.com/posts/284164/edit | How do you handle saving blog tags in MVC?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Data; | |
using System.Data.Entity; | |
using System.Linq; | |
using System.Net; | |
using System.Web; | |
using System.Web.Mvc; | |
using BlogEngine.Models; | |
using BlogEngine.Database.Repositories; | |
using BlogEngine.Database; | |
namespace BlogEngine.Areas.Admin.Controllers | |
{ | |
public class PostsController : Controller | |
{ | |
private ApplicationDbContext db = new ApplicationDbContext(); | |
// GET: Admin/Posts | |
public ActionResult Index() | |
{ | |
return View(db.Posts.ToList()); | |
} | |
// GET: Admin/Posts/Details/5 | |
public ActionResult Details(int? id) | |
{ | |
if (id == null) | |
{ | |
return new HttpStatusCodeResult(HttpStatusCode.BadRequest); | |
} | |
Post post = db.Posts.Find(id); | |
if (post == null) | |
{ | |
return HttpNotFound(); | |
} | |
return View(post); | |
} | |
public ActionResult Create() | |
{ | |
return View(); | |
} | |
// POST: Posts/Create | |
// To protect from overposting attacks, please enable the specific properties you want to bind to, for | |
// more details see http://go.microsoft.com/fwlink/?LinkId=317598. | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
[ValidateInput(false)] | |
public ActionResult Create([Bind(Include = "Id,Title,URL,IntroText,Body,Created,Modified,Author,Tags")] Post post) | |
{ | |
if (true) /*ModelState.IsValid*/ | |
{ | |
var replace = post.Body; | |
replace = replace.Replace("<p><img src=\"../", "<div class=\"center-img\"><img class=\"img-responsive\" src=\"/"); | |
replace = replace.Replace("/></p>", "/></div>"); | |
using (UnitOfWork uwork = new UnitOfWork()) | |
{ | |
var newPost = new Post | |
{ | |
Title = post.Title, | |
URL = post.URL, | |
IntroText = post.IntroText, | |
Body = replace, | |
Author = post.Author, | |
}; | |
// this is where i left off; I am not sure if I should be handling | |
// this in the controller or in the view... I would think the controller | |
// would be the place to do it. Maybe I should pass a string back from the | |
// view??? | |
// this is wrong, it ends up saving System.Collections.Generic.List`1[BlogEngine.Models.Tag] to SQL Server | |
IList<Tag> postTags = new List<Tag>(); | |
var s = post.Tags.ToString(); | |
string[] newTags = s.Split(','); | |
foreach (var val in newTags) | |
{ | |
Tag iTag = new Tag | |
{ | |
Name = val | |
}; | |
uwork.TagRepository.Insert(iTag); | |
postTags.Add(iTag); | |
} | |
newPost.Tags = postTags; | |
uwork.PostRepository.Insert(newPost); | |
uwork.Commit(); | |
return RedirectToAction("Index", "Dashboard"); | |
} | |
} | |
return View(post); | |
} | |
// GET: Admin/Posts/Edit/5 | |
public ActionResult Edit(int? id) | |
{ | |
if (id == null) | |
{ | |
return new HttpStatusCodeResult(HttpStatusCode.BadRequest); | |
} | |
Post post = db.Posts.Find(id); | |
if (post == null) | |
{ | |
return HttpNotFound(); | |
} | |
return View(post); | |
} | |
// POST: Admin/Posts/Edit/5 | |
// To protect from overposting attacks, please enable the specific properties you want to bind to, for | |
// more details see http://go.microsoft.com/fwlink/?LinkId=317598. | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public ActionResult Edit([Bind(Include = "Id,Title,URL,IntroText,Body,Created,Modified,Author")] Post post) | |
{ | |
if (ModelState.IsValid) | |
{ | |
db.Entry(post).State = EntityState.Modified; | |
db.SaveChanges(); | |
return RedirectToAction("Index"); | |
} | |
return View(post); | |
} | |
// GET: Admin/Posts/Delete/5 | |
public ActionResult Delete(int? id) | |
{ | |
if (id == null) | |
{ | |
return new HttpStatusCodeResult(HttpStatusCode.BadRequest); | |
} | |
Post post = db.Posts.Find(id); | |
if (post == null) | |
{ | |
return HttpNotFound(); | |
} | |
return View(post); | |
} | |
// POST: Admin/Posts/Delete/5 | |
[HttpPost, ActionName("Delete")] | |
[ValidateAntiForgeryToken] | |
public ActionResult DeleteConfirmed(int id) | |
{ | |
Post post = db.Posts.Find(id); | |
db.Posts.Remove(post); | |
db.SaveChanges(); | |
return RedirectToAction("Index"); | |
} | |
protected override void Dispose(bool disposing) | |
{ | |
if (disposing) | |
{ | |
db.Dispose(); | |
} | |
base.Dispose(disposing); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment