I have a view that displays a list of comments. He does this through a DisplayTemplate. All I need to do is something like @Html.DisplayFor(x => x.BlogPost.PostComments) , and all comments are displayed accordingly.
There is a form at the bottom of the page to add a new comment. This page uses progressive enhancement. Therefore, if javascript is disabled, the form is submitted as usual, adds a comment to the database, and then redirects to the action that displays the blog post. However, if javascript is available, jQuery captures the submit form and makes the message through ajax. Good, because the markup of the comments is in the display template, I donβt know how to return it from the action method so that jQuery can delete it on the page.
I know how to do this with partial representations. I just would like the action method to return the correct partial view, and jquery will add a response to the comment container on the page.
Before I start cutting out my display template in favor of a partial view, is there a direct path that I donβt have enough to send the display template from the controller?
Here is my action method:
public ActionResult AddComment(PostComment postComment) { postComment.PostedDate = DateTime.Now; postCommentRepo.AddPostComment(postComment); postCommentRepo.SaveChanges(); if (Request.IsAjaxRequest()) return ??????? else return RedirectToAction("BlogPost", new { Id = postComment.BlogPostID }); }
When a page loads, it need not be worried because it uses templates in a standard way:
<div class="comments"> @Html.DisplayFor(x => x.BlogPost.BlogPostComments) </div>
I just want to know how I can send one comment that uses the display template back in jQuery.
jquery c # asp.net-mvc asp.net-mvc-3 razor
Chev
source share