【问题标题】:ActionLink across controllers跨控制器的 ActionLink
【发布时间】:2012-08-11 15:54:44
【问题描述】:

我正在尝试在我的控制器 RequestedService 的视图中创建一个操作链接,该链接将用户带到另一个控制器和表单中的操作:

/ItemsForService/Create/{id}

ItemsForService 是 create 函数所在的控制器。

到目前为止,我有:

@Html.ActionLink("Add Item", "ItemsForService", "Create", New With {.id = currentItem.RequestedServiceId})

但是这似乎将其作为 Create?=id 而不是 Create/id 传递

我将如何通过后者?

【问题讨论】:

  • 您能告诉我们您的全局文件中与此相关的路线吗?

标签: asp.net-mvc vb.net asp.net-mvc-3 razor


【解决方案1】:

使用正确的 ActionLink 重载来获得预期的结果

@Html.ActionLink("Add Item", "ItemsForService", "Create", New With { id = currentItem. currentItem }, Nothing)

你不需要.id = ... 它只是id = ...

重载为ActionLink(string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)

当您向操作提供参数时,必须将Nothing 的最后一个参数添加为空HTML 属性。或者,如果您确实需要对链接应用 HTML 属性,您可以使用:

@Html.ActionLink("Add Item", "ItemsForService", "Create", New With { id = currentItem. currentItem }, New With { @class = "MyCustomCssClassName" } )

【讨论】:

  • 我添加了以下 @Html.ActionLink("Add Item", "ItemsForService", "Create", New With {id = currentItem.RequestedServiceId }, null) 但现在得到以下 2错误:匿名类型成员名称只能从不带参数的简单名称或限定名称中推断出来。 &'null' 未声明。不再支持“Null”常量;请改用“System.DBNull”。
  • 我使用的是 c# 版本,所以在 VB 中你可能不想要 Null,我会更新它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2015-01-04
  • 2017-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多