渲染动作返回View();形式问题

发布于 2024-08-17 06:35:03 字数 1305 浏览 4 评论 0原文

我是 MVC 新手,所以请耐心等待。 :-)

我有一个强类型的“故事”视图。该视图(故事)可以有评论。

我为我的评论控制器“ListStoryComments”和“CreateStoryComment”创建了两个视图(不是部分视图),它们执行其名称所暗示的功能。这些视图使用 RenderAction 包含在故事视图中,例如:(

<!-- 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 }); %>

我传入故事 id 以便列出相关评论)。

一切都如我所愿,除了当我使用表单发布新评论时,它返回当前(父)视图,但评论表单字段仍然显示我输入的最后内容,并且 ListStoryComments 视图未更新为展示新故事。

基本上,页面是从缓存加载的,就像我按下了浏览器的后退按钮一样。如果我按 f5,它将尝试重新发布表单。如果我手动重新加载页面(在浏览器地址栏中重新输入 URL),然后按 f5,我将看到新内容和空表单字段,这就是我想要的结果。

为了完整起见,我的 CreateStoryComment 操作如下所示:

    [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();
        }
    }

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();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

じее 2024-08-24 06:35:05

由于问题似乎是缓存,您可以简单地禁用/限制缓存。

将以下属性添加到您的操作中:

[OutputCache(Duration = 0, VaryByParam = "none")]

这将告诉浏览器缓存页面,但缓存时间为 0 秒。当帖子重新加载页面时,您应该会看到所需的结果。

Since the problem seems to be caching, you can simply disable/limit caching.

Add the following attribute to your actions:

[OutputCache(Duration = 0, VaryByParam = "none")]

This will tell the browser to cache the page, but for 0 seconds. When the post reloads the page, you should see the desired results.

-残月青衣踏尘吟 2024-08-24 06:35:05

答案是确保您的表单操作设置正确。如果您使用了 renderaction 并且没有手动设置表单控制器和操作,则操作将是当前 URL。

尝试:

<% using (Html.BeginForm("ActionName", "ControllerName")) {%>

而不是:

<% using (Html.BeginForm()) {%>

The answer is to make sure your form action is properly set. If you have used renderaction and not set the form controller and action manually then the action will be the current URL.

Try:

<% using (Html.BeginForm("ActionName", "ControllerName")) {%>

Instead of:

<% using (Html.BeginForm()) {%>
剑心龙吟 2024-08-24 06:35:04

答案是使用return RedirectToAction()。使用它可以强制执行 PRG 模式并实现我的目标。

我之前对原始帖子的评论确实导致了一个错误,我想我仍然对此感到困惑,但这有效:

return RedirectToAction("Details", "Steps", new { id = "2" });

The answer is to use return RedirectToAction(). Using this enforces the PRG pattern and achieves my goal.

My earlier comment to my original post did cause an error, that I guess I'm still confused about, but this works:

return RedirectToAction("Details", "Steps", new { id = "2" });
天暗了我发光 2024-08-24 06:35:04

我,这是个人意见,认为你解决这个问题的方式是错误的。

我个人会;

  1. 创建一个名为 ListStories 的视图。
  2. 创建一个列出了的部分视图
    故事。
  3. 创建局部视图来创建
    故事。
  4. 当您想添加故事时,只需
    显示添加故事 html。
  5. 然后当用户按下按钮时
    你做了一个 jQuery 回发,添加
    新故事并返回 PartialView
    要么是新故事,要么是所有
    故事。
  6. 如果您返回全部的部分视图
    然后故事取代边界
    包含所有故事的 div
    与新数据。
  7. 如果您只返回一个故事
    然后将其附加到div的末尾
    包含故事。

我知道这意味着大量的返工,听起来很复杂,需要大量的工作,但这样做意味着以后有更大的灵活性,因为您可以重复使用部分视图,或者可以使用一次和所有视图进行更改该部分视图现已更新。

另外,使用 jQuery 意味着添加故事看起来似乎没有任何明显的回发,这很好。

I, and this is a personal opinion, think you've tackled this the wrong way.

Personally I would;

  1. Create a view called ListStories.
  2. Create a partial view that lists the
    stories.
  3. Create a partial view to create a
    story.
  4. When you want to add a story, simply
    show the add story html.
  5. Then when the user presses a button
    you do a jQuery postback, add the
    new story and return a PartialView
    of either the new story or all the
    stories.
  6. If you return a partial view of all
    stories then replace the bounding
    div that contains all the stories
    with the new data.
  7. If you return only a single story
    then append it to the end of the div
    containing the stories.

I know this means a lot of re-work and it sounds complex and like a lot of work but doing it like this means greater flexibility later on because you can re-use the partial views or you can make a change once and all views using that partial view are now updated.

also, using jQuery means that the adding of stories looks seemless w/out any obvious post back which is nice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文