Html.ActionLinkとは
ASP.NETでハイパーリンクを生成するためのヘルパーです。
@Html.ActionLink
いっつもコピーして使うので、パラメータなどの例をまとめました。
@Html.ActionLink("リンク名", "ActionName") //同じコントローラの場合
@Html.ActionLink("リンク名", "ActionName", "ControllerName ") //異なるコントローラの場合
@Html.ActionLink("リンク名", "ActionName", "ControllerName ", new { id = Id },null) //ルートやパラメータの指定
@Html.ActionLink("リンク名", "ActionName", "ControllerName ", null, new { @class= "class" }) //htmlのclassは付けたいけどルートやパラメータがない場合
@Html.ActionLink("リンク名", "ActionName", "ControllerName ", new { id = Id }, new { @class= "class" }) //フルセット!
リンクパスのみ返す場合は@Url.Actionを使用します。
classは予約語なので、@でエスケープする必要がある。
@Url.Action
タグの中に書きたい場合はこれです。
<a href="@Url.Action("ActionName", "ControllerName ")">リンク名</a>
<a href="@Url.Action("ActionName", "ControllerName ", new { id = Id })" class="class">リンク名</a>
Html.ActionLinkジェネレーター
便利なことにフォームに入力すると、該当箇所が置換されます。
@Html.ActionLink("リンク名", "ActionName")
@Html.ActionLink("リンク名", "ActionName", "ControllerName ")
@Html.ActionLink("リンク名", "ActionName", "ControllerName ", new { id = Id },null)
@Html.ActionLink("リンク名", "ActionName", "ControllerName ", null, new { @class= "class" })
@Html.ActionLink("リンク名", "ActionName", "ControllerName ", new { id = Id }, new { @class= "class" })
<a href="@Url.Action("ActionName", "ControllerName ")">リンク名</a>
<a href="@Url.Action("ActionName", "ControllerName ", new { id = Id })">リンク名</a>