Asp.net-Mvc-3

如何在 ActionLink 中包含書籤/片段?

  • December 12, 2012

可能重複:

在 asp.net mvc Html.ActionLink 中包含錨標記

編碼 :@Html.ActionLink("Link", "Action", "Controller", new { id = Id } )

目前我可以生成這樣的連結:

http://mywebsite/Controller/Action/Id

我想生成這樣的連結:

http://mywebsite/Controller/Action/Id#divId

但我無法編輯路線/創建另一條路線。

什麼是最好的解決方案?

只需使用proper overloadActionLink 助手:

@Html.ActionLink(
   linkText: "Link",
   actionName: "Action",
   controllerName: "Controller",
   protocol: null,
   hostName: null,
   fragment: "divId",
   routeValues: new { id = Id },
   htmlAttributes: null
)

將產生:

<a href="/Controller/Action/123#divId">Link</a>

引用自:https://stackoverflow.com/questions/13835536