asp.net-mvc - MVC Controller 被调用两次

标签 asp.net-mvc

我有一个带有 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/

相关文章:

c# - OnActionExecuting 未在引用的 MVC 项目中执行

iis - MSdeploy 使用错误的虚拟目录名称部署 MVC 2 应用程序

c# - 如何开发将模型对象转换为 Excel 工作表 (.xls) 和文本文件 (.csv) 的操作方法

标题元素中的 ASP.NET 换行符

mysql - 使用 MySQL,具有固定名称 'MySql.Data.MySqlClient' 的 ADO.NET 提供程序未在计算机或应用程序配置文件中注册

c# - 如何禁用 ASP.NET MVC url 路由

.net - .cshtml Razor 文件中的 TypeScript

c# - 多个 DbContext 会产生死锁吗?

asp.net-mvc - 将存储过程与具有附加参数的 Linq To Sql 一起使用

asp.net-mvc - 在 IIS7 中运行 ASP.NET MVC 时出现问题