你走在正确的轨道上。我不确定为什么它对你不起作用,因为你的代码有一个拼写错误,会产生一个} expected
错误。以下是您正在寻找的内容:
<%= Html.ActionLink("Test Link", "SomeAction", "SomeController",
null, new {id = "someID" }) %>
生成以下 HTML:
<a href="/SomeController/SomeAction" id="someID">Test Link</a>
Edit:我刚刚意识到问题是什么,因为我误读了您尝试的内容。您使用了错误的重载来传递id
html 元素。您可能会通过new { id="blah" }
参数进入routeValues
参数,这将导致在构建路由链接时使用它,而不是htmlAttributes
参数就是你想要的。
我认为你正在使用:
ActionLink(string linkText, string actionName, Object routeValues,
Object htmlAttributes)
当您需要使用以下重载时,就像我在上面的回答中所做的那样:
ActionLink(string linkText, string actionName, string controllerName,
Object routeValues, Object htmlAttributes)
这确保了new { id="blah" }
正在被传递到htmlAttributes
param.