Render action return View(); form problem

Posted by Roger Rogers on Stack Overflow See other posts from Stack Overflow or by Roger Rogers
Published on 2010-01-07T17:19:26Z Indexed on 2010/03/30 17:03 UTC
Read the original article Hit count: 356

Filed under:
|

I'm new to MVC, so please bear with me. :-)

I've got a strongly typed "Story" View. This View (story) can have Comments.

I've created two Views (not partials) for my Comments controller "ListStoryComments" and "CreateStoryComment", which do what their names imply. These Views are included in the Story View using RenderAction, e.g.:

<!-- List comments -->
<h2>All Comments</h2>
<% Html.RenderAction("ListStoryComments", "Comments", new { id = Model.Story.Id }); %>

<!-- Create new comment -->
<% Html.RenderAction("CreateStoryComment", "Comments", new { id = Model.Story.Id }); %>

(I pass in the Story id in order to list related comments).

All works as I hoped, except, when I post a new comment using the form, it returns the current (parent) View, but the Comments form field is still showing the last content I typed in and the ListStoryComments View isn’t updated to show the new story.

Basically, the page is being loaded from cache, as if I had pressed the browser’s back button. If I press f5 it will try to repost the form. If I reload the page manually (reenter the URL in the browser's address bar), and then press f5, I will see my new content and the empty form field, which is my desired result.

For completeness, my CreateStoryComment action looks like this:

    [HttpPost]
    public ActionResult CreateStoryComment([Bind(Exclude = "Id, Timestamp, ByUserId, ForUserId")]Comment commentToCreate)
    {
        try
        {
            commentToCreate.ByUserId = userGuid;
            commentToCreate.ForUserId = userGuid;
            commentToCreate.StoryId = 2; // hard-coded for testing

            _repository.CreateComment(commentToCreate);
            return View();
        }
        catch
        {
            return View();
        }
    }

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about renderaction