ASP.NET MVC:html.actionlink()生成空链接
-
03-10-2019 - |
题
好吧,我在ActionLink HTMLHELPER上遇到了一些问题。
我有一些复杂的路由如下:
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"});
我的问题是生成匹配这些模式的动作链接。 EventName参数实际上只是为了拥有一个用户友好的链接。它无能为力。
现在,当我尝试生成链接时。这显示了某个凹槽的仪表板。喜欢:
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的新手。有人可以告诉我我在做什么错吗?
帮助您将不胜感激!
解决方案
除了已经指出的内容外,这里还有很多错误 - 您还围绕错误的方式有控制器和动作字符串。
您所追求的这个方法签名是这样的:
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) %>
hths,
查尔斯
其他提示
我认为问题在于它找不到匹配这些参数的路由。您已经拼错了GroupID,并且您输入了您要匹配的路由中不存在的路由参数(“ EventName_url”)。 ActionLink可能应该看起来像这样:
<%: Html.ActionLink("Show dashboard", "Group", "Dashboard", new { EventName = "test", GroupID = item.groepID}, null)%
不隶属于 StackOverflow