渲染动作返回View();形式问题
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于问题似乎是缓存,您可以简单地禁用/限制缓存。
将以下属性添加到您的操作中:
这将告诉浏览器缓存页面,但缓存时间为 0 秒。当帖子重新加载页面时,您应该会看到所需的结果。
Since the problem seems to be caching, you can simply disable/limit caching.
Add the following attribute to your actions:
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.
答案是确保您的表单操作设置正确。如果您使用了 renderaction 并且没有手动设置表单控制器和操作,则操作将是当前 URL。
尝试:
而不是:
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:
Instead of:
答案是使用return RedirectToAction()。使用它可以强制执行 PRG 模式并实现我的目标。
我之前对原始帖子的评论确实导致了一个错误,我想我仍然对此感到困惑,但这有效:
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:
我,这是个人意见,认为你解决这个问题的方式是错误的。
我个人会;
故事。
故事。
显示添加故事 html。
你做了一个 jQuery 回发,添加
新故事并返回 PartialView
要么是新故事,要么是所有
故事。
然后故事取代边界
包含所有故事的 div
与新数据。
然后将其附加到div的末尾
包含故事。
我知道这意味着大量的返工,听起来很复杂,需要大量的工作,但这样做意味着以后有更大的灵活性,因为您可以重复使用部分视图,或者可以使用一次和所有视图进行更改该部分视图现已更新。
另外,使用 jQuery 意味着添加故事看起来似乎没有任何明显的回发,这很好。
I, and this is a personal opinion, think you've tackled this the wrong way.
Personally I would;
stories.
story.
show the add story html.
you do a jQuery postback, add the
new story and return a PartialView
of either the new story or all the
stories.
stories then replace the bounding
div that contains all the stories
with the new data.
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.