asp.net-mvc MVC 控制器被调用两次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2751138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC controller is being called twice
提问by rboarman
I have a controller that is being called twice from an ActionLink call.
我有一个从 ActionLink 调用中被调用两次的控制器。
My home page has a link, that when clicked calls the Index method on the Play controller. An id of 100 is passed into the method. I think this is what is causing the issue. More on this below.
我的主页有一个链接,单击该链接时会调用 Play 控制器上的 Index 方法。将 100 的 id 传递到该方法中。我认为这就是导致问题的原因。更多关于这个下面。
Here are some code snippets:
下面是一些代码片段:
Home page:
主页:
<%= Html.ActionLink("Click Me", "Index", "Play", new { id = 100 }, null) %>
Play Controller:
播放控制器:
public ActionResult Index(int? id)
{
var settings = new Dictionary<string, string>();
settings.Add("Id", id.ToString());
ViewData["InitParams"] = settings.ToInitParams();
return View();
}
Play view:
播放视图:
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
(html <head> omitted for brevity)
<body>
<form id="form1" runat="server" style="height:100%">
Hello
</form>
</body>
If I get rid of the parameter to the Index method, everything is fine. If I leave the parameter in place, then the Index method is called with 100 as the id. After returning the View, the method is called a second time with a parameter of null.
如果我去掉 Index 方法的参数,一切都很好。如果我将参数保留在原位,则使用 100 作为 id 调用 Index 方法。返回 View 后,该方法被第二次调用,参数为 null。
I can't seem to figure out what is triggering the second call.
我似乎无法弄清楚是什么触发了第二个电话。
My first thought was to add a specific route like this:
我的第一个想法是添加这样的特定路线:
routes.MapRoute(
"Play", // Route name
"Play/{id}", // URL with parameters
new {controller = "Play", action = "Index"} // Parameter defaults
);
This had no effect other than making a prettier looking link.
除了制作更漂亮的链接之外,这没有任何影响。
I am not sure where to go from here.
我不知道从这里去哪里。
回答by Frank Schwieterman
Is there any other markup that could be accidentally referencing the page? Script references, image references, css references, all could be mistakenly pointed at '.' or the current page.
是否有任何其他标记可能会意外引用该页面?脚本引用、图像引用、css 引用,都可能被错误地指向“.”。或当前页面。
回答by Claire
10 hours chasing that bug in a Java Spring Maven project.
在 Java Spring Maven 项目中追逐该错误 10 个小时。
First on SELECT I thought Hibernate was just logging twice, but then with INSERT I thought requests were called twice. Stepping throught the code I discovered controller was called twice...
首先在 SELECT 上,我认为 Hibernate 只是记录了两次,但是对于 INSERT,我认为请求被调用了两次。单步执行我发现控制器被调用两次的代码......
Tried all possible Spring configuration, thinking the context was loaded twice or a bean was instantiate twice...
尝试了所有可能的 Spring 配置,认为上下文被加载了两次或者一个 bean 被实例化了两次......
In despair, rebuilded the project piece by piece to finally add a fragment of HTML and kaboom bug's back.
无奈之下,把项目一点一点的重建,最后加了一段HTML和kaboom bug的背影。
<img alt="" src="#" />
The sharp sign was guilty, reloading the URL. I know the topic is old, but I summarized my searchs with the words I used to look for in vain on Internet to find an answer to the same issue! Could help others...
尖锐的标志是有罪的,重新加载了 URL。我知道这个话题很老,但我用我以前在互联网上徒劳地寻找的词来总结我的搜索,以找到同一问题的答案!可以帮助别人...
回答by Alistair
You can step through the code in your view. Step through and see where the second call comes from.
您可以单步执行视图中的代码。逐步查看第二个呼叫的来源。
回答by usefulBee
While debugging I found out that a Partial View causes the the Controller to be called a second time. It sucks, but I don't see a work around that one.
在调试时,我发现部分视图会导致控制器第二次被调用。这很糟糕,但我没有看到解决这个问题的方法。
回答by vicky
there should be a html markeup that is not working properly. please check all img tage. also check
应该有一个无法正常工作的 html 标记。请检查所有 img 标签。还检查
<link rel="icon" href="favicon.ico" type="image/x-icon" />
回答by Lester
Try changing the int? id
to int id
. It's matching the route the 2nd time because you're calling the index again with a null id.
尝试将 更改int? id
为int id
。它第二次匹配路由,因为您再次使用空 ID 调用索引。
回答by Mike Geise
You can also try changing your route to this.
您也可以尝试将您的路线更改为此。
routes.MapRoute(
"Play", // Route name
"Play/{id}", // URL with parameters
new { controller = "Play", action = "Index" , id = "" } // Parameter defaults
);
回答by JTinTX
Something very stupid I did...had 2 Forms on a page with one button on each form. I had added script to submit a specific form based on the button clicked but, since the default action of a button on a form is to do a submit, it called my Controller Action twice. :^(
我做了一些非常愚蠢的事情......在一个页面上有 2 个表单,每个表单上有一个按钮。我添加了脚本以根据单击的按钮提交特定表单,但是由于表单上按钮的默认操作是提交,因此它两次调用了我的控制器操作。:^(
$('#SaveButton').click(function (event) {
$("#AttendanceDetailForm").submit();
});
回答by HorsinAround
In my case, I was using a Partial View (so no Form tags) and using an on click handler in JQuery to call a method in the controller via Ajax. But I had declared the button the handler was attached to as type Submit. I forgot to pass in e to my handler function so as to call e.PreventDefault()!! So the Controller method was being called twice - once from an ajax call and once for Submit. The 2nd time around the parameters were null. This caused me so much grief. Such a small thing. So small that it was easily overlooked. Hope this helps someone else.
就我而言,我使用的是局部视图(因此没有表单标签)并使用 JQuery 中的点击处理程序通过 Ajax 调用控制器中的方法。但是我已将处理程序附加到的按钮声明为类型 Submit。我忘了将 e 传递给我的处理程序函数以便调用 e.PreventDefault() !!所以 Controller 方法被调用了两次 - 一次来自 ajax 调用,一次用于提交。第二次参数为空。这让我非常伤心。这么小的事情。太小了,很容易被忽视。希望这对其他人有帮助。
回答by rns
I was also facing the same issue. after thoroughly checking my project , i found none such empty reference case. Then i found out that it is caused by FireBug. Disabling firebug or using other browser which has not firebug installed solved the problem.
我也面临同样的问题。在彻底检查我的项目后,我没有发现这样的空参考案例。然后我发现它是由FireBug引起的。禁用萤火虫或使用其他未安装萤火虫的浏览器解决了问题。