1

Taking first steps with ASP.NET MVC, I am trying to create a simple (and typical) article-with-comments page: under an article itself there should be a form enabling an user to post a comment to the article.

I created a partial view for the submit form and CommentController with following methods:

public ActionResult Add(int entryId);

[HttpPost]
public ActionResult Add(Comment comment);

Then, under the article in a view of HomeController:

<div class="add-comment">
    @{ Html.RenderAction("Add", "Comment", new { entryId = Model.EntryId }); }
</div>

The form renders properly and the adding procedure actually works (comment gets saved into database), but after redirecting back to the article InvalidOperationException is thrown, with Html.RenderAction (the one shown above) highlited in debugger:

System.InvalidOperationException: Child actions are not allowed to perform redirect actions.

Why does it happen?

Here's the code for CommentController methods:

public ActionResult Add(int entryId)
{
    var comment = new Comment { EntryId = entryId };
    return PartialView(comment);
}

[HttpPost]
public ActionResult Add(Comment comment)
{
    if (ModelState.IsValid)
    {
        comment.Date = DateTime.Now;
        var entry = db.Entries.FirstOrDefault(e => e.EntryId == comment.EntryId);
        if (entry != null)
        {
            entry.Comments.Add(comment);
            db.SaveChanges();
            return RedirectToAction("Show", "Home", new { id = entry.EntryId });
        }
    }

    return PartialView(comment);
}

Or maybe should I even take a diffrent approach?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Clueless
  • 605
  • 3
  • 10
  • 19
  • This answer probably has an explenation. I see that I have upvoted it at some point so it has helped me at least :) http://stackoverflow.com/questions/2056421/why-are-redirect-results-not-allowed-in-child-actions-in-asp-net-mvc-2 – juhan_h Jul 27 '13 at 08:10
  • 1
    Where is the action method of getting comments from database ? – Imad Alazani Jul 27 '13 at 08:37
  • There is none. Model class passed to view (action Show) has a collection of comments as a navigation property, so displaying them wasn't a problem. – Clueless Jul 27 '13 at 16:57
  • @Doe, could you add the code for the `Show` action of the `Home` controller? Also, could you post the stack trace for the error? It might tell us in which controller is the error happening. – Amith George Jul 28 '13 at 16:07
  • 1
    @Doe You can't use return RedirectToAction("Show", "Home", new { id = entry.EntryId }); because you are in ChildAction, this is the problem. This logic should not be in child action, should be in parent. – Alberto León Jul 28 '13 at 16:12
  • @AlbertoLeón, the OP seems to be following the standard Post-Redirect-Get pattern. The `RedirectToAction` is needed here. – Amith George Jul 28 '13 at 23:42

2 Answers2

0

Add HttpGet on the other Add action

0

You should/could be using RenderPartial instead of RenderAction:

Html.RenderPartial("YourPartialView", new Comment { EntryId = Model.EntryId });

There appears to be no need to use your action method if all you are doing is instantiating a model that you already have the ID too.

marteljn
  • 6,446
  • 3
  • 30
  • 43