asp.net-mvc 将 Html.ActionLink 与 RouteValues 一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8369334/
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
Using Html.ActionLink with RouteValues
提问by seanfitzg
I have the following Html:
我有以下 Html:
<%: Html.ActionLink(item.ProductName, "Details", "Product", new { item.ProductId }, null)%>
This is being rendered as:
这被呈现为:
<a href="/Product/Details?ProductId=1">My Product Name</a>
However, when I click on this, I get the following error:
但是,当我单击它时,出现以下错误:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'MyProject.Controllers.ProductController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
参数字典包含“MyProject.Controllers.ProductController”中方法“System.Web.Mvc.ActionResult Details(Int32)”的不可为空类型“System.Int32”的参数“id”的空条目。可选参数必须是引用类型、可为空类型或声明为可选参数。
参数名称:参数
It appears that my routing doesn't like the "?ProductId=1" query string.
看来我的路由不喜欢“?ProductId=1”查询字符串。
If I use instead:
如果我改用:
<%: Html.ActionLink(item.ProductName, string.Format("Details/{0}", item.ProductId), "Product", null, null)%>
I get the following link rendered:
我得到以下链接呈现:
<a href="/Product/Details/1">My Product Name</a>
...and this works correctly when clicked.
...单击时可以正常工作。
Am I missing something basic here? I'd like to use RouteValues, but I don't understand why this error is being thrown. How can I get my Controller method to accept query string parameters?
我在这里错过了一些基本的东西吗?我想使用 RouteValues,但我不明白为什么会抛出这个错误。如何让我的 Controller 方法接受查询字符串参数?
The only route mapping I have is:
我唯一的路由映射是:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
采纳答案by gdoron is supporting Monica
Change the action parameter to be int ProductId.
将 action 参数更改为 int ProductId。
public ActionResult Details(int productId)
{
return View("");
}
your controller have to get an "id" parameter because you declared it as notnullable int so when you send productId it still doesn't match the function signature.
when you don't specify the parameter name, the routing defaults in the global.asax change the parameter name to id:
您的控制器必须获得一个“id”参数,因为您将其声明为不可为空的 int,因此当您发送 productId 时,它仍然与函数签名不匹配。
当不指定参数名时,global.asax中的路由默认将参数名改为id:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
see the last line.
见最后一行。
回答by Galled
You are setting the /character as separators between the controller, the action and the id (the parameters) if you call a url like /Product/Details?ProductId=1you are calling the controller Productbut the action with the text Details?ProductId=1and then routing hasn't get the next /.
/如果您调用 url 就像/Product/Details?ProductId=1调用控制器一样,则将字符设置为控制器、操作和 id(参数)之间的分隔符,Product但是带有文本Details?ProductId=1和路由的操作没有得到下一个/.

