我有一个带有 Index Action 的 Product Controller ,它基本上为 ProductController 上的 post 和 Index(Post Action 动词) Action 创建 View 表单,它基本上将产品保存到 db 但是当发生验证错误时,我返回一个 View(mymodel) else保存后,我返回 RedirectToAction("Created,"Product") 但由于某些奇怪的原因,当我进入代码时,它击中了 Product Controller 操作两次而不是一次。因此该产品有 2 条记录而不是一条记录。
public ActionResult Index()
{
return View()
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection fc)
{
// 2 calls are made to this controller
try
{
// save the product
return RedirectToAction("Created");
}
catch(Exception ex)
{
// recreate the model from form collection
return View(viewData); // when a validation error occurs it comes into the catch block
}
}
最佳答案
有时我发现 Firebug 会导致这种行为。尝试禁用它的 Script panel ,如果你安装了它。
说明:在某些情况下,Firebug 无法在其“脚本”面板中获取显示的脚本源。在这些情况下,它会发起第二个请求来获取它们。见 issue 7401对此进行一些讨论,这减轻了问题并在 Firebug 2.0.2 中得到了修复。
关于asp.net-mvc - MVC Controller 被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1068938/