【发布时间】:2012-05-04 23:09:57
【问题描述】:
我在完成 POST 操作后尝试 RedirectToAction 时遇到 NullReferenceException。这是我正在尝试的摘要:
应用程序发布后,如果应用程序有效,我希望客户端重定向到另一个视图(和路由)后续流程。
[HttpPost]
public ActionResult SubmitApplication(Application application)
{
// Do stuff
if (!ModelState.IsValid)
{
return View(application);
}
// Create and set variables for the next route
return RedirectToAction("ApplicationReview", new
{
x = "foo",
y = "bob"
});
}
[HttpGet]
public ActionResult ApplicationReview(string x, string y)
{
// Do stuff using x and y as keys
return View();
}
在 SubmitApplication 视图中,我有类似于以下的代码,尽管它更详细。 RedirectToAction 被调用后,SubmitApplication 视图中的Model 对象为空并抛出异常。我可以理解为什么 SubmitApplication 视图作为重定向过程的一部分返回,我只是无法弄清楚为什么模型为空或如何设置它。最后,可能永远不会到达目标 Action,因为重定向从未真正发生过。
@if (Model.HasSomething)
{
...
}
我做错了什么?有没有更好的方法来做到这一点?
【问题讨论】:
标签: asp.net-mvc