为DisplayFor值创建ActionLink

时间:2014-07-29 04:14:04

标签: c# asp.net-mvc-5 actionlink html.actionlink displayfor

我编写了一个C#MVC 5互联网应用程序,并对ActionLink中的View提出了疑问。

我有以下代码:

<td>
    @Html.DisplayFor(modelItem => item.mapLocations.Count)
</td>

是否可以为上面显示的值创建ActionLink

提前致谢

修改

我尝试过以下代码:

@Html.ActionLink(item.mapLocations.Count, "Index", "MapLocation", new { mapCompanyid = item.Id }, null)

我收到如下编译错误:

Compiler Error Message: CS1928: 'System.Web.Mvc.HtmlHelper<CanFindLocation.ViewModels.MapCompaniesViewModel>' does not contain a definition for 'ActionLink' and the best extension method overload 'System.Web.Mvc.Html.LinkExtensions.ActionLink(System.Web.Mvc.HtmlHelper, string, string, string, object, object)' has some invalid arguments

EDIT2

我是否需要DisplayFor代码才能在ActionLink中显示值?

也许是这样的:

@Html.ActionLink(Html.DisplayFor(modelItem => item.mapLocations.Count), "Index", "MapLocation", new { mapCompanyid = item.Id }, null)

1 个答案:

答案 0 :(得分:1)

这将有效: -

@Html.ActionLink(item.mapLocations.Count, "// action name //", "// controller name //", new{ mapCompanyid = item.Id  }, new{ // html attributes // })

只需匹配您的Actionlink,其结构应如下所示: -

Html.ActionLink(item.mapLocations.Count, //  <--Title
            "Index",   // <-- ActionMethod
            "MapLocation",  // <-- Controller Name.
            new { mapCompanyid = item.Id }, // <-- Route arguments.
            null  // <-- htmlArguments.
            )
相关问题