2010-01-03 53 views
14

這裏是我的代碼添加URL片段與MVC ActionLink的

<%= Html.ActionLink(Model[x].Title, "Index", "q", new { slug = Model[x].TitleSlug, id = Model[x].PostID }, null)%> 

產生這個網址

http://localhost:61158/q/is_there_another_indiana_jones_movie_in_the_works/4 

一部分,但我想製作一個URL片段,像這樣:

http://localhost:61158/q/is_there_another_indiana_jones_movie_in_the_works/4#1 

有沒有辦法使用HTML.ActionLink函數來做到這一點?

+0

如果有人知道一個更好的方式來解釋這種隨意編輯這個問題 – Luke101 2010-01-03 20:49:49

回答

21

有跡象表明,採取分段參數的兩個「巨型重載」的ActionLink的:

public static string ActionLink(this HtmlHelper htmlHelper, 
    string linkText, string actionName, string controllerName, 
    string protocol, string hostName, string fragment, object routeValues, 
    object htmlAttributes); 
public static string ActionLink(this HtmlHelper htmlHelper, 
    string linkText, string actionName, string controllerName, 
    string protocol, string hostName, string fragment, 
    RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes); 

有關重載的詳細信息,請參閱MSDN

你的情況,這將是(和特別注意到了「片段」參數):

<%= Html.ActionLink(Model[x].Title, "Index", "q", 
    /* protocol */ null, /* hostName */ null, /* fragment */ "1", 
    new { slug = Model[x].TitleSlug, id = Model[x].PostID }, null) %> 

隨着「超級重載」你可以離開最參數值無效,他們將獲得相應默認值。