【问题标题】:What is the syntax for including model properties in a Html.ActionLink?在 Html.ActionLink 中包含模型属性的语法是什么?
【发布时间】:2015-06-04 06:08:07
【问题描述】:

例如,假设我在 foreach 中有以下代码来生成表。在其中一列中有一个“单击此处查看更多详细信息”类型的链接。 PurchaseOrderNumber 应该创建一个指向详细信息页面的超链接并传入 PurchaseOrderNumber。示例:/Sales/Details/7000

    <td>
        @Html.ActionLink("Details", "Index", @Html.DisplayFor(m => item.PurchaseOrderNumber))
    </td>

相反,它只是创建指向 /Sales?Length=5 的超链接

我该如何正确地做到这一点?

【问题讨论】:

    标签: c# html asp.net-mvc


    【解决方案1】:

    假设你的控制器名称是Sales,动作名称是Details,参数名称是id

    @Html.ActionLink("Click here for more details",
                     "Details",
                     "Sales",
                     new { id = item.PurchaseOrderNumber }, null)
    

    MSDN

    【讨论】:

    • 这仍然为我生成与以前相同的 URL
    • 控制器名称、动作和参数名称是什么?
    • 就像您在代码中使用它们一样,但它只是创建“Length=5”,无论这意味着什么。
    • 错误重载 - 您需要为 html 属性添加第 5 个参数 - @Html.ActionLink("Click here for more details", "Details", "Sales", new { id = item.PurchaseOrderNumber }, null)
    猜你喜欢
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多