繁体   English   中英

使用对象名称作为与ActionLink的链接,而不是硬编码字面量

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

我试图让我的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. 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.

格式是func,其输入参数的类型为dynamic,并且作为item的结果类型。名称在编译时也是动态的。 如错误所述,请使用以下代码:

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

尝试交换参数?

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时,也可以使用命名参数。

试图寻找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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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