public ActionResult Create(Posts posts) { posts.DateCreated = DateTime.Now; posts.FK_Type = 2; posts.FK_User = (int)Membership.GetUser().ProviderUserKey; if (posts.FK_Status == 0) { posts.FK_Status = db.Categories.FirstOrDefault().Id; } db.Posts.Add(posts); if(Request.Form["imagestoadd"] != null) { string mediasquery = Request.Form["imagestoadd"].Substring(0, Request.Form["imagestoadd"].Length - 1); string[] medias = mediasquery.Split(','); foreach (string itm in medias) { int id = int.Parse(itm); if (db.MediaInPosts.Where(p => p.Fk_Posts == posts.Id && p.Fk_Media == id).Count() == 0) { MediaInPosts AddMedia = new MediaInPosts(); AddMedia.Fk_Media = id; AddMedia.Fk_Posts = posts.Id; db.MediaInPosts.Add(AddMedia); } } } if (Request.Form["Category"] != null) { string[] categories = Request.Form["Category"].Split(','); foreach (string itm in categories) { int id = int.Parse(itm); if (db.CategoriesInPosts.Where(p => p.FK_Posts == posts.Id && p.FK_Categories == id).Count() == 0) { CategoriesInPosts AddCategory = new CategoriesInPosts(); AddCategory.FK_Categories = id; AddCategory.FK_Posts = posts.Id; db.CategoriesInPosts.Add(AddCategory); } } } //Delete All Tags found for the post foreach (TagsInPosts tagini in db.TagsInPosts.Where(p => p.FK_Posts == posts.Id).ToList()) { db.TagsInPosts.Remove(tagini); } //is any tags have been givin if (posts.tags != null) { //Tags string TagTrim = posts.tags.ToString().Replace(" ", string.Empty).ToLower(); string[] tags = TagTrim.Split(','); foreach (string tag in tags) { if (db.Tags.Where(p => p.Name.Contains(tag)).Count() == 0) { DA.Tags addtag = new DA.Tags(); addtag.Name = tag.ToLower(); db.Tags.Add(addtag); db.SaveChanges(); DA.TagsInPosts AddToItem = new DA.TagsInPosts(); AddToItem.FK_Posts = posts.Id; AddToItem.FK_Tag = addtag.Id; db.TagsInPosts.Add(AddToItem); db.SaveChanges(); } else { //Get Id of Tag int Tagid = db.Tags.Where(p => p.Name.Contains(tag)).FirstOrDefault().Id; if (db.TagsInPosts.Where(p => p.FK_Tag == Tagid && p.FK_Posts == posts.Id).Count() == 0) { DA.TagsInPosts AddToItem = new DA.TagsInPosts(); AddToItem.FK_Posts = posts.Id; AddToItem.FK_Tag = db.Tags.Where(p => p.Name.Contains(tag)).FirstOrDefault().Id; db.TagsInPosts.Add(AddToItem); db.SaveChanges(); } } } } db.SaveChanges(); return RedirectToAction("Index"); }
public ActionResult Edit(Posts posts) { if (ModelState.IsValid) { posts.FK_User = (int)Membership.GetUser().ProviderUserKey; db.Entry(posts).State = EntityState.Modified; db.SaveChanges(); if (Request.Form["imagestoadd"] != null) { string mediasquery = Request.Form["imagestoadd"].Substring(0, Request.Form["imagestoadd"].Length - 1); string[] medias = mediasquery.Split(','); foreach (string itm in medias) { int id = int.Parse(itm); if (db.MediaInPosts.Where(p => p.Fk_Posts == posts.Id && p.Fk_Media == id).Count() == 0) { MediaInPosts AddMedia = new MediaInPosts(); AddMedia.Fk_Media = id; AddMedia.Fk_Posts = posts.Id; db.MediaInPosts.Add(AddMedia); } } } foreach (CategoriesInPosts catinpost in db.CategoriesInPosts.Where(p => p.FK_Posts == posts.Id).ToList()) { db.CategoriesInPosts.Remove(catinpost); } db.SaveChanges(); string[] categories = Request.Form["Category"].Split(','); foreach (string itm in categories) { int id = int.Parse(itm); if (db.CategoriesInPosts.Where(p => p.FK_Posts == posts.Id && p.FK_Categories == id).Count() == 0) { CategoriesInPosts AddCategory = new CategoriesInPosts(); AddCategory.FK_Categories = id; AddCategory.FK_Posts = posts.Id; db.CategoriesInPosts.Add(AddCategory); } } //Delete All Tags found for the post foreach (TagsInPosts tagini in db.TagsInPosts.Where(p => p.FK_Posts == posts.Id).ToList()) { db.TagsInPosts.Remove(tagini); } db.SaveChanges(); //is any tags have been givin if (posts.tags != null) { //Tags string TagTrim = posts.tags.ToString().Replace(" ", string.Empty).ToLower(); string[] tags = TagTrim.Split(','); foreach (string tag in tags) { if (db.Tags.Where(p => p.Name.Contains(tag)).Count() == 0) { Tags addtag = new Tags(); addtag.Name = tag.ToLower(); db.Tags.Add(addtag); TagsInPosts AddToItem = new TagsInPosts(); AddToItem.FK_Posts = posts.Id; AddToItem.FK_Tag = addtag.Id; db.TagsInPosts.Add(AddToItem); db.SaveChanges(); } else { //Get Id of Tag int Tagid = db.Tags.Where(p => p.Name.Contains(tag)).FirstOrDefault().Id; if (db.TagsInPosts.Where(p => p.FK_Tag == Tagid && p.FK_Posts == posts.Id).Count() == 0) { TagsInPosts AddToItem = new TagsInPosts(); AddToItem.FK_Posts = posts.Id; AddToItem.FK_Tag = db.Tags.Where(p => p.Name.Contains(tag)).FirstOrDefault().Id; db.TagsInPosts.Add(AddToItem); db.SaveChanges(); } } } } return RedirectToAction("Index"); } ViewBag.FK_Type = new SelectList(db.PostType, "Id", "Name", posts.FK_Type); ViewBag.FK_Status = new SelectList(db.Status, "Id", "Name", posts.FK_Status); return View(posts); }