public async Task<ActionResult> CommentLike(CommentLikeModel model) { if (!ModelState.IsValid) { return RedirectToAction("Post", new { id = model.PostId }); } var blogContext = new BlogContext(); // XXX WORK HERE // Increment the Likes field for the comment at {model.Index} // inside the post {model.PostId}. // // NOTE: The 2.0.0 driver has a bug in the expression parser and // might throw an exception depending on how you solve this problem. // This is documented here along with a workaround: // https://jira.mongodb.org/browse/CSHARP-1246 await blogContext.Posts.UpdateOneAsync( p => p.Id == model.PostId, Builders<Post>.Update.Inc(string.Format("Comments.{0}.Likes", model.Index), 1)); return RedirectToAction("Post", new { id = model.PostId }); }
public async Task<ActionResult> CommentLike(CommentLikeModel model) { if (!ModelState.IsValid) { return RedirectToAction("Post", new { id = model.PostId }); } var blogContext = new BlogContext(); // XXX WORK HERE // Increment the Likes field for the comment at {model.Index} // inside the post {model.PostId}. // // NOTE: The 2.0.0 driver has a bug in the expression parser and // might throw an exception depending on how you solve this problem. // This is documented here along with a workaround: // https://jira.mongodb.org/browse/CSHARP-1246 var post = await blogContext.Posts.Find(p => p.Id == model.PostId).SingleAsync(); post.Comments[model.Index].Likes += 1; await blogContext.Posts.FindOneAndReplaceAsync(p => p.Id == model.PostId, post); return RedirectToAction("Post", new { id = model.PostId }); }