Note that there are some explanatory texts on larger screens.

plurals
  1. PORedirection error
    primarykey
    data
    text
    <p>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. </p> <p>I created a partial view for the submit form and <code>CommentController</code> with following methods:</p> <pre><code>public ActionResult Add(int entryId); [HttpPost] public ActionResult Add(Comment comment); </code></pre> <p>Then, under the article in a view of <code>HomeController</code>:</p> <pre><code>&lt;div class="add-comment"&gt; @{ Html.RenderAction("Add", "Comment", new { entryId = Model.EntryId }); } &lt;/div&gt; </code></pre> <p>The form renders properly and the adding procedure actually works (comment gets saved into database), but after redirecting back to the article <code>InvalidOperationException</code> is thrown, with <code>Html.RenderAction</code> (the one shown above) highlited in debugger:</p> <blockquote> <p>System.InvalidOperationException: Child actions are not allowed to perform redirect actions.</p> </blockquote> <p>Why does it happen?</p> <p>Here's the code for <code>CommentController</code> methods:</p> <pre><code>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 =&gt; e.EntryId == comment.EntryId); if (entry != null) { entry.Comments.Add(comment); db.SaveChanges(); return RedirectToAction("Show", "Home", new { id = entry.EntryId }); } } return PartialView(comment); } </code></pre> <p>Or maybe should I even take a diffrent approach?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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