【问题标题】:Create an ActionLink for a DisplayFor value为 DisplayFor 值创建 ActionLink
【发布时间】:2014-09-20 09:39:18
【问题描述】:

我编写了一个 C# MVC 5 Internet 应用程序,并且对 View 中的 ActionLink 有疑问。

我有以下代码:

<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)

【问题讨论】:

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


    【解决方案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.
                )
    

    【讨论】:

    • item.mapLocations.Count 处于循环中,因此无法正常工作。
    • 你想要item.mapLocations.Count 在title 还是在actionlink 中的route 参数???
    • 我想将 Count 值显示为操作链接的文本。你能看看我的编辑吗?
    • 我已经编写了代码,但是我的帖子中出现了错误。
    • 这是不可能的...试试@item.mapLocations.Count 而不是item.mapLocations.Count
    猜你喜欢
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    • 2016-12-28
    • 1970-01-01
    相关资源
    最近更新 更多