MVC控制器被调用两次 [英] MVC controller is being called twice
问题描述
我有一个正在被从ActionLink的电话叫了两声控制器。
I have a controller that is being called twice from an ActionLink call.
我的主页上有一个链接,该按钮时调用播放控制器上的指数方法。 100的ID被传递到方法。我认为这是什么原因造成的问题。更多关于这下面。
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.
下面是一些code片段:
Here are some code snippets:
主页:
<%= Html.ActionLink("Click Me", "Index", "Play", new { id = 100 }, null) %>
播放器:
public ActionResult Index(int? id)
{
var settings = new Dictionary<string, string>();
settings.Add("Id", id.ToString());
ViewData["InitParams"] = settings.ToInitParams();
return 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>
如果我摆脱参数对指数的方法,一切都很好。如果我离开这个参数的地方,那么索引方法被调用100作为ID。返回查看后,该方法被称为第二次与空的参数。
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.
我似乎无法找出是什么触发了第二个电话。
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
);
这有比发出prettier寻找其他链接没有任何影响。
This had no effect other than making a prettier looking link.
我不知道在哪里可以从这里走。
I am not sure where to go from here.
感谢您提前。
里克
推荐答案
是否有任何其他标记,可能是不小心引用的页面?脚本引用,引用图像,CSS参考,所有可能被错误地在指出'。或当前页面。
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.
这篇关于MVC控制器被调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!