Asp.net mvc 向Html助手MVC4生成的Html表添加操作链接

Asp.net mvc 向Html助手MVC4生成的Html表添加操作链接,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我有一个Htmlhelper扩展方法,可以从任何列表中生成一个Html表 我必须添加功能,使来自任何给定列的数据能够成为链接 我创建了一个新类,其中包含创建链接所需的所有数据 public class ColumnLinkDescription { //controller name public string Controller { get; set; } //Action name public string Action { get; set; }

我有一个Htmlhelper扩展方法,可以从任何
列表中生成一个Html表

我必须添加功能,使来自任何给定列的数据能够成为链接

我创建了一个新类,其中包含创建链接所需的所有数据

  public class ColumnLinkDescription
  {
    //controller name
    public string Controller { get; set; }
    //Action name
    public string Action { get; set; }
    //parameter
    public string ID { get; set; }
  }
我还添加了一个方法,如果列具有链接描述,则该方法将尝试生成链接

    private static string TryGenerateLink<T>(HtmlHelper helper, T d, TableHeaderDetails h, string value)
    {
      if (h.Link != null)
      {
        var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
        var url = urlHelper.Action(h.Link.Controller,
        h.Link.Action,
        new { id = d.GetType().GetProperty(h.Link.ID) });
        value= url;
      }
      return value;
    }
看起来我应该使用与urlHelper.Action不同的方法,我很难获得ArticleCode的值并将其作为参数添加到链接中

编辑1:

我通过在TryGenerateLink()中进行简单修改获得了参数的值

输出:

<td class=" ">/ActionTest/ControllerTest/96776</td>
/ActionTest/ControllerTest/96776

因此,剩下的唯一问题是正确生成超链接

您不想为此使用
UrlHelper
,因为您已经注意到,
UrlHelper.Action
将创建相对URL,但不会创建锚定标记。您真正想做的是利用
HtmlHelper
。这样,您就可以使用
helper.ActionLink
为您构建链接

<td class=" ">/ActionTest/ControllerTest/Int32%20ArticleCode</td>
new ColumnLinkDescription{Controller = "ControllerTest", Action="ActionTest", ID="ArticleCode"}
var url = urlHelper.Action(h.Link.Controller, h.Link.Action, new { id = d.GetType().GetProperty(h.Link.ID).GetValue(d,null) });
<td class=" ">/ActionTest/ControllerTest/96776</td>