Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 1.41 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. No data is saved in the DB and no errors is fired
  2. public class Post
  3. {
  4.     [Key]
  5.     public int PostID { get; set; }
  6.     ...
  7.     public virtual ICollection<Tag> Tags { get; set; }
  8. }
  9.        
  10. public class Tag
  11. {
  12.     [Key]
  13.     public int TagID { get; set; }
  14.  
  15.     [Required, StringLength(50)]
  16.     public string Name { get; set; }
  17.  
  18.     public virtual ICollection<Post> Posts { get; set; }
  19. }
  20.        
  21. Tag t = m_TagRepository.GetTag(tag.Trim().ToUpper());
  22.     if (t == null) t = new Tag { Name = tag.Trim().ToUpper() };
  23.     post.Tags.Add(t);
  24.        
  25. public void SavePost(Post post)
  26.     {
  27.         if (post.PostID == 0)
  28.         {
  29.             m_Context.Posts.Add(post);
  30.         }
  31.         else
  32.         {
  33.             var entry = m_Context.Entry(post);
  34.             entry.State = EntityState.Modified;
  35.         }            
  36.         m_Context.SaveChanges();
  37.     }
  38.        
  39. [Authorize, HttpPost, ValidateInput(false), Theme("Admin")]
  40.     public ActionResult Edit(PostFullViewModel postToEdit)
  41.     {
  42.         if (!ModelState.IsValid)
  43.             return View();
  44.  
  45.         Post post = Mapper.Map<PostFullViewModel, Post>(postToEdit);
  46.         m_PostBusiness.UpdateTags(post, postToEdit.TagString);
  47.         m_PostBusiness.SavePost(post);
  48.         TempData.SetStatusMessage(Strings.Post_SavedSuccessfully);
  49.  
  50.         return RedirectToAction("Manage");
  51.     }
  52.        
  53. var post = GetPostFromYourRequest();
  54. context.Posts.Attach(post);
  55. ProcessTags(post, postToEdit);
  56. context.SaveChanges();