【问题标题】:How to invoke an MVC action method from a href in an anchor?如何从锚点中的 href 调用 MVC 操作方法?
【发布时间】:2016-08-25 22:29:16
【问题描述】:

我使用的标记如下。

@Html.ActionLink(@User.Identity.Name, "LogOut", "Account")

现在,我需要在锚点内添加一个跨度,因为我想使用 Bootstrap 中的字形。就我的 googlearch 而言,没有办法使用上面的帮助程序来指定它。因此,我将其重新设计为显式 HTML,如下所示。

<a href="~/Account/LogOut">
  <span class="glyphicon glyphicon-log-out"></span>
  <span>@Global.LogOut @User.Identity.Name</span>
</a>

它可以工作链接并不总是指向与第一个示例相同的地址。这是因为我首先有 en/se/ 等语言。基于 MVC 路由时,语言前缀保持不变,但在显式指定 URL 时会丢失。

如何指定对特定操作方法的调用?

【问题讨论】:

  • 使用&lt;a href = "@Url.Action("LogOut", "Account")">....`

标签: c# html asp.net-mvc


【解决方案1】:

试试:

<a href="@Url.Action("LogOut", "Account")">
  <span class="glyphicon glyphicon-log-out"></span>
  <span>@Global.LogOut @User.Identity.Name</span>
</a>

【讨论】:

  • 虽然不正确,但我会赞成。链接中的文本没有参数。只有动作和控制器。
  • 无法获得任何功劳。编译器和智能感知告诉我...... :)
【解决方案2】:

在视图中,您可以使用如下参数调用 MVC 操作:

<a href="@Url.Action("FilterCustomerGrid", "Customer", new { filterName = "CustomerID", filterValue = @Model.ID })">Click Here</a>;

在你的控制器端

public ActionResult FilterCustomerGrid(string filterName, string filterValue)
{
 return View("ImportExcel");
}

【讨论】:

    猜你喜欢
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    相关资源
    最近更新 更多