简体   繁体   中英

Use Object Name as Link with ActionLink, Not Hard-coded Literal

I'm trying to get my WebGrid to use the name of my entity as a link. If I just do this:

grid.Column("Name"),

The grid displays the name of the entity in each row of the grid:

在此处输入图片说明

However, I want the name to appear as a link. The closest I've come to getting this working is doing this:

grid.Column("Name", format: (item) => @Html.ActionLink("Edit", "Edit", new { id = item.Id })),

在此处输入图片说明

But, as you can see, every name is Edit. How can I get the actual object name there? I tried this, but I get an error (the only difference is that I'm trying to use item.Name in place of "Edit" as the first parameter of the ActionLink method):

grid.Column("Name", format: (item) => @Html.ActionLink(item.Name, "Edit", new { id = item.Id })),

Error: TrackerJob>>' has no applicable method named 'ActionLink' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax. TrackerJob>>' has no applicable method named 'ActionLink' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

format is func which have input parameter of type dynamic and as result type of item.Name is dynamic too at compile time. And as error says use following code :

grid.Column("Name", format: (item) => @Html.ActionLink((string)item.Name, "Edit", new { id = item.Id })

Try swapping the arguments around?

grid.Column("Name", format: (item) => @Html.ActionLink("Edit", item.Name, new { id = item.Id }))

After First Comment

Try using an overload of ActionLink with more parameters ( from here ):

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName,
    RouteValueDictionary routeValues,
    IDictionary<string, Object> htmlAttributes
)

As an overload can become ambiguous with just strings. Alternatively use named parameters when defining the ActionLink.

Tried looking into GetSelectLink() ? More on it here: http://weblogs.asp.net/andrebaltieri/archive/2010/11/02/asp-net-mvc-3-working-with-webgrid-part-2.aspx

Personally, I would stick with making the tables yourself - when they get more customized, I find it less confusing, but that is my opinion.

EDIT: You could do something like this, but again I stress that you are kind of getting away from the point of this being 'simple':

item.GetSelectLink(String.Format("<a href='{0}'>{1}</a>", Url.Action("Edit", new { id = item.Id }), item.Name))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM