ASP.NET MVC 3 Adding Comments to an Article View

I have an article model with public ICollection<Comment> Comments { get; set; } public ICollection<Comment> Comments { get; set; } public ICollection<Comment> Comments { get; set; } and the comment model. I created a presentation for the article (view details), and I want to show everything from the model article (not a problem) and comments on the article in and after the comment, and then show the form for adding a comment to the article (not on another page, I want to so that she was viewed with the article). At the moment I have this:

 @model SkMoravanSvitavka.Models.Article @{ ViewBag.Title = "Zobrazit"; } <h2>Zobrazit</h2> <fieldset> <legend>Article</legend> <div class="display-label">Title</div> <div class="display-field">@Model.Title</div> <div class="display-label">Text</div> <div class="display-field">@Model.Text</div> <div class="display-label">PublishedDate</div> <div class="display-field">@String.Format("{0:g}", Model.PublishedDate)</div> </fieldset> @if (Model.Comments != null) { foreach (var comment in Model.Comments) { @Html.Partial("_Comment", comment) } } <p> @Html.ActionLink("Edit", "Edit", new { id = Model.ArticleID }) | @Html.ActionLink("Back to List", "Index") </p> 

This shows the article, and there is a partial view for all comments on the article. And now I'm not sure how to add a form to add comments. thank you

Change Here is my comment controller and create methods (vytvorit = create in czech :)):

  public ActionResult Vytvorit(int articleID) { var newComment = new Comment(); newComment.articleID = articleID; // this will be sent from the ArticleDetails View, hold on :). newComment.Date = DateTime.Now; return View(newComment); } [HttpPost] public ActionResult Vytvorit(Comment commentEntity) { db.Comments.Add(commentEntity); db.SaveChanges(); return RedirectToAction("Zobrazit", "Clanek", new { id = commentEntity.articleID }); } 

When I change @Html.RenderAction to @Html.Action , it works. It shows a text box for comments, and I can add a comment, but there is a problem that it doesn’t just add a text box, but it adds my site again (not just a partial view, but all the views), and I'm sure that I add Create view for comment as partial.

+4
source share
2 answers

Create a new partial view, make it a strongly typed one of the type comments.

From the scaffolding templates, select the Create template.

handle normal add a new comment script.

Add this partial view to the article details page.

Please note that when you are going to save a new comment, you will need to get the ID of the hosting article.

Hopefully now it is clear, if not, let me know.

update: by assuming that you add a partial "AddComment" view to the "Article Details" view, you can do the following to add a comment.

1- Change the GET (Create) action inside CommentController as follows:

 public ActionResult Create(int articleID) { var newComment = new CommentEntity(); newComment.articleID = articleID; // this will be sent from the ArticleDetails View, hold on :). return View(newComment); } 

1- perform the POST action (Create) as follows:

 [HttpPost] public ActionResult Create(Comment commentEntity) { // Complete the rest.. } 

2- A partial view of the comment will look something like this:

 @model NGO.Models.Comment @using (Html.BeginForm()) { @Html.ValidationSummary(true) <div class="addcommentbox"> <h2> Add Comment </h2> @Html.TextAreaFor(model => model.Description) <div class="ErrorMessage"> @Html.ValidationMessageFor(model => model.Description) </div> <input id="addComment" type="button" onclick="" value="Add" /> </div> } 

3 on the ArticleDetails page in the right place where you need the comment section to show, use the RenderAction method to render the AddComment Partial view as follows:

 Html.RenderAction("Create", "Comment",new {articleID = Model.ID}); 

the previous line will call the GET (Create) action inside CommentColtroller and pass the current article identifier, so the already filled in current article identifier will appear in the AddComment Partial view (and this is what we want).

so that he does not hesitate to ask me if he is not yet clear, and let me know if this works for you.

+7
source

I highly recommend that you create a view model for your article. as below:

 public class ArticleViewModel { public Article _article {get;set;} //this is for the comment submit section public Comment _comment {get;set;} //this for the comments that you will view public IQueryable<Comment> _comment {get;set;} } 

after that, pass this to your view from your controller and make your view strongly typed for this ArticleViewModel class.

Create a section that is wrapped inside the form tag, as shown below:

 @using(Html.BeginForm()){ @*Put your code inside here. Create inputs for sending comments. like below;*@ @Html.TextboxFor(Model._comment.Content) } 

and then create a method for this;

 [HttpPost] [ActionName("name_of_your_article_page_action")] public ActionResult Create(Comment commentEntity) { } 

NOTE. Of course, you do not need to create a separate view model. you can hardcode the name of your inputs, but this makes it difficult to use validation and other things. but it’s not impossible to use validation though!

+1
source

Source: https://habr.com/ru/post/1345519/


All Articles