Html.ActionLink

 

Html.ActionLink 擴充方法列表

名稱
ActionLink(HtmlHelper, String linkText, String actionName)
ActionLink(HtmlHelper, String linkText, String actionName, Object routeValues)
ActionLink(HtmlHelper, String linkText, String actionName, String controllerName)
ActionLink(HtmlHelper, String linkText, String actionName, RouteValueDictionary routeValues)
ActionLink(HtmlHelper, String linkText, String actionName, Object routeValues, Object htmlAttributes)
ActionLink(HtmlHelper, String linkText, String actionName, RouteValueDictionary routeValues, IDictionary<String, Object> htmlAttributes)
ActionLink(HtmlHelper, String linkText, String actionName, String controllerName, Object routeValues, Object htmlAttributes)
ActionLink(HtmlHelper, String linkText, String actionName, String controllerName, RouteValueDictionary routeValues, IDictionary<String, Object> htmlAttributes)
ActionLink(HtmlHelper, String linkText, String actionName, String controllerName, String protocol, String hostName, String fragment, Object routeValues, Object htmlAttributes)
ActionLink(HtmlHelper, String linkText, String actionName, String controllerName, String protocol, String hostName, String fragment, RouteValueDictionary routeValues, IDictionary<String, Object> htmlAttributes)

只舉幾個重要範例

一、ActionLink(HtmlHelper, String linkText, String actionName, Object routeValues)

@Html.ActionLink("首頁", "Index", new { id = 1 })

二、ActionLink(HtmlHelper, String linkText, String actionName, RouteValueDictionary routeValues)

@{
    ViewBag.Title = "Home Page";
    RouteValueDictionary RouteValueDictionary = new RouteValueDictionary();
    RouteValueDictionary.Add("id", 1);
}

@Html.ActionLink("首頁", "Index", RouteValueDictionary)

三、ActionLink(HtmlHelper, String linkText, String actionName, Object routeValues, Object htmlAttributes)

@Html.ActionLink("首頁", "Index", new { id = 1 }, new { @class = "myclass", id = "myid" })

HTML原始碼

<a class="myclass" href="/Home/Index/1" id="myid">首頁</a>

四、ActionLink(HtmlHelper, String linkText, String actionName, RouteValueDictionary routeValues, IDictionary<String, Object> htmlAttributes)

可以用任何有實作IDictionary介面的類別去作htmlAttributes參數,如使用Dictionary

@using System.Collections.Generic;

@{
    ViewBag.Title = "Home Page";
    RouteValueDictionary RouteValueDictionary = new RouteValueDictionary();
    RouteValueDictionary.Add("id", 1);

    Dictionary<string, object> Dictionary = new Dictionary<string, object>() {
        { "id", "myid" },
        { "class","myclass"}
    };
}

@Html.ActionLink("首頁", "Index", RouteValueDictionary, Dictionary)

HTML原始碼

<a class="myclass" href="/Home/Index/1" id="myid">首頁</a>

五、ActionLink(HtmlHelper, String linkText, String actionName, String controllerName, String protocol, String hostName, String fragment, RouteValueDictionary routeValues, IDictionary<String, Object> htmlAttributes)

@using System.Collections.Generic;

@{
    ViewBag.Title = "Home Page";
    RouteValueDictionary RouteValueDictionary = new RouteValueDictionary();
    RouteValueDictionary.Add("id", 1);

    Dictionary<string, object> Dictionary = new Dictionary<string, object>() {
        { "id", "myid" },
        { "class","myclass"}
    };
}

@Html.ActionLink("首頁", "Index", "Home", "http", "brooke.twgg.org", "main", RouteValueDictionary, Dictionary)

HTML原始碼

<a class="myclass" href="http://brooke.twgg.org:61556/Home/Index/1#main" id="myid">首頁</a>