C# Html.ActionLink 从不同文件夹中的不同控制器调用操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17625246/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:04:00  来源:igfitidea点击:

Html.ActionLink to call an action from a different controller that's in a different folder

c#asp.net-mvcasp.net-mvc-4actionlink

提问by SantasNotReal

I've got a simple view that creates a link if a login is successful and is located under /Login:

我有一个简单的视图,如果登录成功并位于 /Login 下,它会创建一个链接:

<div>
@Html.ActionLink("Add a new Organization", "AddOrganization", 
           "/Setup/AddOrganizationController", new { id = Session["ID"] }, null)
</div>

After reading other similiar problems, I tried it adding the null after, as well as a few other overloads, but I can't get the link to work right. When I click the link, it takes me to

在阅读了其他类似问题后,我尝试在之后添加空值以及其他一些重载,但我无法使链接正常工作。当我点击链接时,它带我到

http://setup/AddOrganizationController/AddOrganization

Which is leaving out the localhost part that needs to be there. Without the null at the end, it tries to send me to

这遗漏了需要在那里的本地主机部分。最后没有空值,它会尝试将我发送到

/Login/AddOrganization

All I want is a link that will run an action within the AddOrganizationController controller which is under /Setup directory. The link should also pass the session id to the controller as an argument. How can I do this?

我想要的只是一个链接,它将在 /Setup 目录下的 AddOrganizationController 控制器中运行一个操作。该链接还应将会话 ID 作为参数传递给控制器​​。我怎样才能做到这一点?

回答by Matt Millican

If it's in the same Areathen you can just do:

如果它是相同的,Area那么你可以这样做:

@Html.ActionLink("Add a new Organization", "AddOrganization", "Organizations", new { id = Session["ID"] })

where "Organizations" is the controller name.

其中“组织”是控制器名称。

Otherwise, if it's in another area you would do something like

否则,如果它在另一个区域,你会做类似的事情

@Html.ActionLink("Add a new Organization", "AddOrganization", "Organizations", new { area = "areaName", id = Session["ID"] }, null)