我有一个通用方法,可以根据当前 Controller 、当前操作和当前可选参数(如果有)生成链接结构。
public static string GetLink(this HtmlHelper helper, RouteData routeData) {
...
foreach(var item in routeData.Values){
if(!item.key.Equals("controller") && !item.key.Equals("action")) {
url = helper.ActionLink("text link", "myAction", "myController",
new { /* here I want to convert item.Key to anonymous param */ = "2" } );
}
}
}
我在那里添加了一条注释 /* 这里我想将 item.Key 转换为匿名参数 */
如何实现这一目标?
谢谢
我解决了问题。请使用 UrlHelper
而不是 HtmlHelper
并生成 RouteValueDictionary
对象来放置匿名可选参数。
最佳答案
尚未对此进行测试,但尝试从您的项目和 key 创建 RouteValueDictionary
。
public static string GetLink(this HtmlHelper helper, RouteData routeData)
{
foreach(var item in routeData.Values)
{
if(!item.Key.Equals("controller") && !item.Key.Equals("action"))
{
var routeValues = new RouteValueDictionary(item);
var url = helper.ActionLink("text link", "myAction", "myController", routeValues, null);
}
}
}
关于c# - 将字符串转换为匿名参数,以从 ActionLink 方法发送到对象 RouteValues (MVC4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19288902/