将字符串转换为要从操作链接方法 (MVC4) 发送到对象路由值的匿名参数
本文关键字:对象 路由 参数 MVC4 转换 字符串 操作 方法 链接 | 更新日期: 2023-09-27 18:34:38
我有一个通用方法,它基于当前控制器、当前动作和当前可选参数(如果有的话(生成链接结构。
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" } );
}
}
}
我在那里放了一个评论/* here I want to convert item.Key to anonymous param */
如何实现这一点?
谢谢
我解决了问题。请使用UrlHelper
而不是HtmlHelper
并生成一个RouteValueDictionary
对象来放置匿名可选参数。
尚未对此进行测试,但请尝试从您的项目和键创建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);
}
}
}