ASP.NET MVC:Html.ActionLink()生成空链接

4
好的,我遇到了一些ActionLink HTML助手的问题。
我的路由比较复杂,如下所示:
        routes.MapRoute("Groep_Dashboard_Route", // Route name
                        "{EventName}/{GroupID}/Dashboard", // url with Paramters
                        new {controller = "Group", action="Dashboard"});

        routes.MapRoute("Event_Groep_Route", // Route name
                        "{EventName}/{GroupID}/{controller}/{action}/{id}",
                        new {controller = "Home", action = "Index"});

我的问题是生成与这些模式匹配的操作链接。 事件名称参数只是为了有一个用户友好的链接,它并没有任何作用。
现在,例如,当我尝试生成一个链接时,它显示某个组的仪表板。 像:
  mysite.com/testevent/20/Dashboard

我将使用以下的ActionLink:
<%: Html.ActionLink("Show dashboard", "Group", "Dashboard",  new { EventName_Url = "test", GroepID = item.groepID}, null)%>

我的 HTML 实际结果是:
 <a href="">Show Dashboard</a>

我应该拥有的是类似于:
 <a href="test/20/Dashboard">Show Dashboard</a>

请容忍我,我还不熟悉ASP MVC。有人能告诉我我做错了什么吗?
非常感谢帮助!
2个回答

3
除了之前指出的问题,这里还有一些其他的问题 - 你把控制器和操作字符串弄反了。
你要找的方法签名应该是这样的:
HtmlHelper.ActionLink(string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)

所以你的需求应该是:
<%: Html.ActionLink("Show dashboard", "Dashboard", "Group", new { EventName = "test", GroupID = item.groupID}, null) %>

大家好,
Charles


是的!拼写错误是因为我想将代码稍微翻译成英文并更清晰地表达。问题确实是我颠倒了控制器和动作。愚蠢的错误。对于这样一个愚蠢的问题打扰了大家,我很抱歉。非常感谢! - user323395
2
伙计,不要对自己太苛刻了...我们都有自己的pebkac时刻!无论问题看起来是多么简单或困难,请继续发布问题 :-) Ps. pebkac = [p]roblem [e]xists [b]etween [k]eyboard [a]nd [c]hair - Charlino

3

我认为问题在于没有找到与这些参数匹配的路由。你拼写了错误的GroupID,并且在尝试匹配的路由中输入了一个不存在的路由参数(“EventName_Url”)。ActionLink应该像这样:

<%: Html.ActionLink("Show dashboard", "Group", "Dashboard",  new { EventName = "test", GroupID = item.groepID}, null)%

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接