I have a Htmlhelper extension method that generates an Html table from any List<T>
I have to add functionality to make the data from any given column the ability to be a Link.
I have created a new class that contains all the data needed to make a link
public class ColumnLinkDescription
{
//controller name
public string Controller { get; set; }
//Action name
public string Action { get; set; }
//parameter
public string ID { get; set; }
}
i also added a method that will try to generate a link if the column has a Link description
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;
}
this ties into my Table maker in the following way:
value= ((d.GetType().GetProperty(h.Name).GetValue(d, null)) ?? "").ToString();
td.InnerHtml = TryGenerateLink<T>(helper, d, h, value);
tr.InnerHtml += td.ToString();
I tried it out but the output was:
<td class=" ">/ActionTest/ControllerTest/Int32%20ArticleCode</td>
using the definition:
new ColumnLinkDescription{Controller = "ControllerTest", Action="ActionTest", ID="ArticleCode"}
It looks like I should be using a differnet approach than urlHelper.Action and I am having a tough time obtaining the value of ArticleCode and adding that as a parameter to the link.
EDIT1:
I got the value of the parameter working by a simple modification in TryGenerateLink()
var url = urlHelper.Action(h.Link.Controller, h.Link.Action, new { id = d.GetType().GetProperty(h.Link.ID).GetValue(d,null) });
Output:
<td class=" ">/ActionTest/ControllerTest/96776</td>
so the only issue remaining is the correct generation of a hyperlink