2013-03-11 76 views
1

怎麼樣<a href="/home/index#item">text</a>與生成鏈接:如何添加#ID到Html.ActionLink

return RedirectToAction()

@Html.ActionLink()

我需要添加#item的標籤頁面加載開放。 Thanx。

回答

5

只需使用ActionLink的幫手proper overload

@Html.ActionLink(
    linkText: "text", 
    actionName: "index", 
    controllerName: "home", 
    protocol: Request.Url.Scheme, 
    hostName: null, 
    fragment: "item", 
    routeValues: null, 
    htmlAttributes: null 
) 

或快捷方式,如果你會:

@Html.ActionLink("text", "index", "home", Request.Url.Scheme, null, "item", null, null) 

,如果你想重定向到這樣的行動,你可以使用GenerateUrl方法:

public ActionResult SomeAction() 
{ 
    string url = UrlHelper.GenerateUrl(
     routeName: null, 
     actionName: "index", 
     controllerName: "home", 
     protocol: null, 
     hostName: null, 
     fragment: "item", 
     routeValues: new RouteValueDictionary(), 
     routeCollection: Url.RouteCollection, 
     requestContext: Url.RequestContext, 
     includeImplicitMvcValues: false 
    ); 
    return Redirect(url); 
}