public ActionResult Update(int?id, string title, string body, DateTime dateTime, string tags) { if (!IsAdmin) { return(RedirectToAction("Index")); } Post post = GetPost(id); post.Title = title; post.Body = body; post.DateTime = dateTime; post.Tag.Clear(); tags = tags ?? string.Empty; string[] tagNames = tags.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); foreach (string tagName in tagNames) { post.Tag.Add(GetTag(tagName)); } if (!id.HasValue) { model.AddToPosts(post); } model.SaveChanges(); return(RedirectToAction("Details", new { id = post.ID })); }
//To enter data into the model public ActionResult Update(int?id, string title, string body, DateTime dateTime, string tags) { if (!IsAdmin) { return(RedirectToAction("Index")); } //Get the POst if it exists Post post = GetPost(id); //Once the Post is obtained, populate the properties of post post.Title = title; post.Body = body; post.DateTime = dateTime; //Tag is a list of tag objects, so clear it first post.Tags.Clear(); //Verify that input sequence of tag names is not Null, then split sequence into a list of tag names tags = tags ?? string.Empty; string[] tagNames = tags.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); //Adding the Tag for each item in the list foreach (string tagName in tagNames) { post.Tags.Add(GetTag(tagName)); } //If this is a new Post, add it if (!id.HasValue) { model.AddToPosts(post); } //save the entries to the database model.SaveChanges(); //Redirect to the Detailed view of the new created Post return(RedirectToAction("Details", new { id = post.ID })); }