0

I'm trying to add an ActionLink to a webgrid column but am running into problems.

If I add the action link like so, it works and links to the main UserDetails page:

grid.Column(header: "", format: (item) => Html.ActionLink("Details", "UserDetails", "Admin"))

However, if I try to pass in the user of the grid item, it returns empty string.

This:

grid.Column(header: "", format: (item) => Html.ActionLink("Details", "UserDetails", "Admin", new { username = item.user }, null))

Results in this:

<a href="">

I've searched this site and have tried some variations but none have worked. Including:

grid.Column(header: "", format: @<text>@Html.ActionLink("Details", "UserDetails", "Admin", new { username = (string)item.user }, null)</text>)

In my controller I have:

public ActionResult UserDetails(string username)
    {    
        return View();
    }

My model for this view and grid is a

List<UserInfo>

where UserInfo has "user" and "activedate".

I'm still new to MVC so I'm hoping there is something silly I'm overlooking.

Thanks.

Ento
  • 43
  • 1
  • 6

1 Answers1

0

ActionLink uses your routing to determine if it can build the link. If not, it sometimes returns nothing. From Reflector (below), notice the htmlHelper.RouteCollection. Check your global.asax (and maybe run your site with the URL you want built), to ensure you routing table will route that Url.

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
    if (string.IsNullOrEmpty(linkText))
    {
        throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");
    }
    return MvcHtmlString.Create(HtmlHelper.GenerateLink(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection, linkText, null, actionName, controllerName, routeValues, htmlAttributes));
}
Geoff Cox
  • 6,102
  • 2
  • 27
  • 30
  • Thank you, that was it. I added the route to my Global.asax and corrected how I was handling the model for UserDetails and it worked. – Ento Aug 14 '12 at 13:55