0

我试图让我的 WebGrid 使用我的实体名称作为链接。如果我这样做:

grid.Column("Name"),

网格在网格的每一行中显示实体的名称:

在此处输入图像描述

但是,我希望名称显示为链接。我最接近这项工作的是这样做:

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

在此处输入图像描述

但是,如您所见,每个名称都是 Edit。我怎样才能在那里获得实际的对象名称?我试过这个,但我得到一个错误(唯一的区别是我试图使用 item.Name 代替“Edit”作为 ActionLink 方法的第一个参数):

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

错误: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.

4

3 回答 3

1

格式是具有动态类型的输入参数和作为项目的结果类型的函数。名称在编译时也是动态的。正如错误所说,使用以下代码:

grid.Column("Name", format: (item) => @Html.ActionLink((string)item.Name, "Edit", new { id = item.Id })
于 2012-08-21T22:19:10.873 回答
0

尝试交换论点?

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

第一次评论后

尝试使用带有更多参数的 ActionLink 重载(来自此处):

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

因为仅使用字符串,重载就会变得模棱两可。或者在定义 ActionLink 时使用命名参数。

于 2012-08-21T20:25:45.423 回答
0

试过调查GetSelectLink()吗?更多信息在这里:http ://weblogs.asp.net/andrebaltieri/archive/2010/11/02/asp-net-mvc-3-working-with-webgrid-part-2.aspx

就个人而言,我会坚持自己制作表格 - 当它们变得更加定制时,我发现它不那么令人困惑,但这是我的观点。

编辑:你可以做这样的事情,但我再次强调你有点远离这个“简单”的点:

item.GetSelectLink(String.Format("<a href='{0}'>{1}</a>", Url.Action("Edit", new { id = item.Id }), item.Name))
于 2012-08-21T20:38:53.040 回答