MVC提供了自動(dòng)構(gòu)造重寫地址的方法——Html.ActionLink,該方法有五個(gè)重載,用起來非常靈活和方便,下面對(duì)這些方法一一介紹
重載一啦扬、Html.ActionLink("linkText","actionName")
該重載的第一個(gè)參數(shù)是該鏈接要顯示的文字,第二個(gè)參數(shù)是對(duì)應(yīng)的控制器的方法視圖,默認(rèn)控制器為當(dāng)前頁面的控制器
例如:
Html.ActionLink("跳轉(zhuǎn)到About頁面", "About");
解析為:
<a href="/Home/About">跳轉(zhuǎn)到About頁面</a>
重載二、 Html.ActionLink("linkText","actionName","controlName")
該重載比第一個(gè)重載多了一個(gè)參數(shù)狸捕。第一個(gè)參數(shù):要顯示的文本,第二個(gè)參數(shù):視圖名, 第三個(gè)參數(shù):控制器名
例如:
Html.ActionLink("跳轉(zhuǎn)到別的controler中", "Index", "Home");
解析為:
<a href="/Home/Index">跳轉(zhuǎn)到別的controler中</a>
重載三、 Html.ActionLik("linkText","actionName",routeValues)
第一個(gè)參數(shù):要顯示的文本,第二個(gè)參數(shù):視圖名, 第三個(gè)參數(shù):url中的參數(shù)
例如:
Html.ActionLink("跳轉(zhuǎn)到About頁面", "About", new { id = "1", name = "x" })
解析為:
<a href="/Home/About/1?name=x">跳轉(zhuǎn)到About頁面</a>
重載四脸侥、 Html.ActionLink("linkText","actionName",routeValues,htmlAttributes)
第一個(gè)參數(shù):要顯示的文本,第二個(gè)參數(shù):視圖名, 第三個(gè)參數(shù):url中的參數(shù),第四個(gè)參數(shù):設(shè)置標(biāo)簽屬性
例如:
<%=Html.ActionLink("跳轉(zhuǎn)到About頁面", "About", new { id = "1", name = "x" }, new { disabled = "disabled",@class = "about"})%>
解析為:
<a class="about" disabled="disabled" href="/Home/About/1?name=x">跳轉(zhuǎn)到About頁面</a>
注意:設(shè)置標(biāo)簽的class屬性時(shí),應(yīng)在class前加上@,因?yàn)閏lass是關(guān)鍵字辛燥。