C# ASP.NET MVC 将 ActionLink 中的 ID 传递给控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/316889/
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
ASP.NET MVC passing an ID in an ActionLink to the controller
提问by Tablet
I can't see to retrieve an ID I'm sending in a html.ActionLink in my controller, here is what I'm trying to do
我看不到检索我在控制器中的 html.ActionLink 中发送的 ID,这就是我想要做的
<li>
<%= Html.ActionLink("Modify Villa", "Modify", "Villa", new { @id = "1" })%></li>
public ActionResult Modify(string ID)
{
ViewData["Title"] =ID;
return View();
}
That's what a tutorial I followed recommended, but it's not working, it's also putting ?Length=5 at the end of the URL!
这就是我推荐的教程,但它不起作用,它还将 ?Length=5 放在 URL 的末尾!
Thanks in advance!
提前致谢!
edit: here is the route I'm using, it's default
编辑:这是我正在使用的路线,它是默认的
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
it appears someone has downvoted the two suggestions below but not posted their solution!
似乎有人对下面的两个建议投了反对票,但没有发布他们的解决方案!
采纳答案by AnthonyWJones
Doesn't look like you are using the correct overload of ActionLink. Try this:-
看起来您没有使用正确的 ActionLink 重载。尝试这个:-
<%=Html.ActionLink("Modify Villa", "Modify", new {id = "1"})%>
This assumes your view is under the /Views/Villa folder. If not then I suspect you need:-
这假设您的视图位于 /Views/Villa 文件夹下。如果没有,那么我怀疑您需要:-
<%=Html.ActionLink("Modify Villa", "Modify", "Villa", new {id = "1"}, null)%>
回答by Davide Vosti
Don't put the @ before the id
不要把@放在id之前
new { id = "1" }
The framework "translate" it in ?Lenght when there is a mismatch in the parameter/route
当参数/路由不匹配时,框架将其“翻译”为 ?Lenght
回答by Oracular Man
In MVC 4 you can link from one view to another controller passing the Id or Primary Key via
在 MVC 4 中,您可以从一个视图链接到另一个通过 Id 或主键传递的控制器
@Html.ActionLink("Select", "Create", "StudentApplication", new { id=item.PersonId }, null)
回答by ebsom
The ID will work with @
sign in front also, but we have to add one parameter after that. that is null
ID 也可以和@
前面的 sign 一起使用,但我们必须在后面添加一个参数。那是null
look like:
看起来像:
@Html.ActionLink("Label Name", "Name_Of_Page_To_Redirect", "Controller", new {@id="Id_Value"}, null)
回答by César León
On MVC 5 is quite similar
在 MVC 5 上非常相似
@Html.ActionLink("LinkText", "ActionName", new { id = "id" })