【发布时间】:2010-01-07 17:19:26
【问题描述】:
我是 MVC 的新手,所以请多多包涵。 :-)
我有一个强类型的“故事”视图。此视图(故事)可以有评论。
我为我的评论控制器“ListStoryComments”和“CreateStoryComment”创建了两个视图(不是部分视图),它们的功能与它们的名称所暗示的一样。这些视图使用 RenderAction 包含在 Story View 中,例如:
<!-- 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 }); %>
(我传入 Story id 以列出相关的 cmets)。
一切都如我所愿,除了,当我使用表单发布新评论时,它返回当前(父)视图,但评论表单字段仍显示我输入的最后内容,而 ListStoryComments 视图不是t 更新以显示新故事。
基本上,页面是从缓存中加载的,就好像我按下了浏览器的后退按钮一样。如果我按 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();
}
}
【问题讨论】:
-
我刚刚在观看 Phil Haack 的另一个 MVC 视频,他提到了“PRG”模式(发布、重定向、获取),我认为应该可以解决这个问题。但是,如果我尝试类似'return RedirectToAction("Index");'我收到“不允许子操作执行重定向操作”的错误,所以我仍然很难过。