asp.net-mvc 在 ASP.NET MVC Html.ActionLink 中包含锚标记
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/274586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Including an anchor tag in an ASP.NET MVC Html.ActionLink
提问by dp.
In ASP.NET MVC, I'm trying to create a link that includes an anchor tag (that is, directing the user to a page, and a specific section of the page).
在 ASP.NET MVC 中,我试图创建一个包含锚标记的链接(即,将用户定向到页面和页面的特定部分)。
The URL I am trying to create should look like the following:
我尝试创建的 URL 应如下所示:
<a href="/category/subcategory/1#section12">Title for a section on the page</a>
My routing is set up with the standard:
我的路由是用标准设置的:
routes.MapRoute("Default", "{controller}/{action}/{categoryid}");
The action link syntax that I am using is:
我使用的操作链接语法是:
<%foreach (Category parent in ViewData.Model) { %>
<h3><%=parent.Name %></h3>
<ul>
<%foreach (Category child in parent.SubCategories) { %>
<li><%=Html.ActionLink<CategoryController>(x => x.Subcategory(parent.ID), child.Name) %></li>
<%} %>
</ul>
<%} %>
My controller method is as follows:
我的控制器方法如下:
public ActionResult Subcategory(int categoryID)
{
//return itemList
return View(itemList);
}
The above correctly returns a URL as follows:
以上正确返回一个 URL,如下所示:
<a href="/category/subcategory/1">Title for a section on the page</a>
I can't figure out how to add the #section12part. The "section" word is just the convention I am using to break up the page sections, and the 12 is the ID of the subcategory, i.e., child.ID.
我不知道如何添加#section12部分。“section”一词只是我用来分解页面部分的约定,12 是子类别的 ID,即 child.ID。
How can I do this?
我怎样才能做到这一点?
采纳答案by LorenzCK
I would probably build the link manually, like this:
我可能会手动构建链接,如下所示:
<a href="<%=Url.Action("Subcategory", "Category", new { categoryID = parent.ID }) %>#section12">link text</a>
回答by Brad Wilson
There are overloads of ActionLink which take a fragmentparameter. Passing "section12" as your fragment will get you the behavior you're after.
ActionLink 有一些重载,它们采用片段参数。传递“section12”作为你的片段会让你得到你所追求的行为。
For example, calling LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, String, String, String, Object, Object):
<%= Html.ActionLink("Link Text", "Action", "Controller", null, null, "section12-the-anchor", new { categoryid = "blah"}, null) %>
回答by PussInBoots
I don't remember in which version of ASP.NET MVC (ASP.NET MVC 3+ I believe) / Razor the parameterlabeldeclaration or whatever it's called (parameter: x) feature was introduced, but to me this is definitely the proper way to build a link with an anchor in ASP.NET MVC.
我不记得在哪个版本的 ASP.NET MVC(我相信 ASP.NET MVC 3+)/ Razor 中引入了参数标签声明或任何它所谓的(参数:x)功能,但对我来说这绝对是正确的方法在 ASP.NET MVC 中建立一个带有锚点的链接。
@Html.ActionLink("Some link text", "MyAction", "MyController", protocol: null, hostName: null, fragment: "MyAnchor", routeValues: null, htmlAttributes: null)
Not even Ed Blackburns antipattern argument from this answercan compete with that.
甚至来自这个答案的Ed Blackburns 反模式论点也无法与之竞争。
回答by Zapnologica
I just did it like this:
我只是这样做:
<a href="@Url.Action("Index","Home")#features">Features</a>
回答by Developer
Here is the real life example
这是现实生活中的例子
@Html.Grid(Model).Columns(columns =>
{
columns.Add()
.Encoded(false)
.Sanitized(false)
.SetWidth(10)
.Titled(string.Empty)
.RenderValueAs(x => @Html.ActionLink("Edit", "UserDetails", "Membership", null, null, "discount", new { @id = @x.Id }, new { @target = "_blank" }));
}).WithPaging(200).EmptyText("There Are No Items To Display")
And the target page has TABS
并且目标页面有 TABS
<ul id="myTab" class="nav nav-tabs" role="tablist">
<li class="active"><a href="#discount" role="tab" data-toggle="tab">Discount</a></li>
</ul>
回答by Ahmed Samir
I Did that and it works for redirecting to other view I think If you add the #sectionLink after It will work
我这样做了,它可以重定向到其他视图,我认为如果在它之后添加 #sectionLink 它将起作用
<a class="btn yellow" href="/users/Create/@Model.Id" target="_blank">
Add As User
</a>
回答by Spikeh
My solution will work if you apply the ActionFilter to the Subcategory action method, as long as you always want to redirect the user to the same bookmark:
如果您将 ActionFilter 应用于 Subcategory 操作方法,我的解决方案将起作用,只要您始终希望将用户重定向到同一个书签:
http://spikehd.blogspot.com/2012/01/mvc3-redirect-action-to-html-bookmark.html
http://spikehd.blogspot.com/2012/01/mvc3-redirect-action-to-html-bookmark.html
It modifies the HTML buffer and outputs a small piece of javascript to instruct the browser to append the bookmark.
它修改 HTML 缓冲区并输出一小段 javascript 以指示浏览器附加书签。
You could modify the javascript to manually scroll, instead of using a bookmark in the URL, of course!
当然,您可以修改 javascript 以手动滚动,而不是在 URL 中使用书签!
Hope it helps :)
希望能帮助到你 :)

