Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I make a news page, with inline comment posting, using MVC3?
    primarykey
    data
    text
    <p>I've been learning MVC3 in the last few weeks and I'm trying to write what I believe should be a simple enough piece of functionality, but I'm having real problems getting it to work correctly.</p> <p><strong>The scenario</strong></p> <p>My website will contain news posts, summarised on the home page, with links to view the full details of each article in it's own page.</p> <p>Comments can be made on each article, and these comments are shown at the end of each article's details page. Authenticated users should be able to post their own comments to the article from the page that shows the article itself. I want to avoid users being taken to a seperate page (although I understand this is easier from a code perspective, I disagree that it's a good UX!).</p> <p>For an example of how I want this to look, Scott Hanselman's blog (<a href="http://www.hanselman.com" rel="nofollow">http://www.hanselman.com</a>) is a perfect example (albeit that his home page shows the entire article rather than just a summary!) - it links through to the comments page, and the "Add comments" form sits neatly at the bottom, inline with the article.</p> <p><strong>Where I'm at so far</strong></p> <p>I've managed to write the Controller for my news section, with an Index method (which retrieves X number of most recent posts) and a "Comments" method to show the full article and comments. I've also created a partal view called "CommentForm" with it's own Controller method. Code snippets from the controller below:</p> <pre><code>// // GET: /News/ public ActionResult Index() { using (var nm = new NewsManager()) { var news = nm.GetLatestActiveNewsArticles(50, "System"); return View(news); } } // // GET: /News/Comments/20 public ActionResult Comments(int id) { using (var nm = new Rapallo.Core.Managers.NewsManager()) { var article = nm.GetArticle(id); return View(article); } } // GET: /News/CommentForm/20 [HttpGet()] public ActionResult CommentForm(int newsId) { return View(new Rapallo.DataAccess.Comment() { NewsId = newsId }); } // // POST: /News/Comments/20 [Authorize()] [HttpPost()] public ActionResult Comments(Comment comment) { using (var nm = new NewsManager()) { var article = nm.GetArticle(comment.NewsId.Value); if (null == article) HttpNotFound(); } using (var cm = new CommentManager()) { if (cm.AddCommentToNews(comment.NewsId.Value, CurrentUser.RapalloUser.Id, comment.Body)) return RedirectToAction("Comments", "News", new { id = comment.NewsId.Value }); } return HttpNotFound(); } </code></pre> <p>And from the Comments view, the code snippet to show where I'd like my add comment form:</p> <pre><code>@if (User.Identity.IsAuthenticated) { @Html.Partial("Comment", new Rapallo.DataAccess.Comment() { NewsId = Model.Id }); } </code></pre> <p>This view specifies:</p> <pre><code>@model Rapallo.DataAccess.News </code></pre> <p>And finally, the comment form itself:</p> <pre><code>@model Rapallo.DataAccess.Comment @using (Html.BeginForm()) { @Html.TextAreaFor(m =&gt; m.Body); &lt;input type="submit" value="Send Comment" /&gt; } </code></pre> <p><strong>The problem</strong></p> <p>In a nutshell, when I submit the comment form (which renders correctly), NewsId on the Comment model is not populated so the controller cannot associate the new comment with the correct news post.</p> <p>I'd appreciate if someone could point out where I'm going wrong! I've tried to adapt some code from NerdDinner to make this work, as well as spending countless hours on the train trying various combinations of partial views, different models, and even renaming methods in my controller in an attempt to get this to work.</p> <p>I'm quite keen on encapsulating the "add comment" functionality into it's own separate form and being able to use this to add comments to more than just news articles. The database schema and domain models already support this, I just can't get the UI to work.</p> <p>Any help would be very welcome and gratefully received.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload