在 ASP.NET MVC Html.ActionLink 中包含锚标记 [英] Including an anchor tag in an ASP.NET MVC Html.ActionLink
问题描述
在 ASP.NET MVC 中,我试图创建一个包含锚标记的链接(即,将用户定向到页面和页面的特定部分).
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).
我尝试创建的 URL 应如下所示:
The URL I am trying to create should look like the following:
<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>
<%} %>
我的控制器方法如下:
public ActionResult Subcategory(int categoryID)
{
//return itemList
return View(itemList);
}
以上正确返回一个 URL 如下:
The above correctly returns a URL as follows:
<a href="/category/subcategory/1">Title for a section on the page</a>
我不知道如何添加 #section12 部分.section"这个词只是我用来分解页面部分的约定,12是子类别的ID,即child.ID.
I can't figure out how to add the #section12 part. 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.
我该怎么做?
推荐答案
我可能会手动构建链接,如下所示:
I would probably build the link manually, like this:
<a href="<%=Url.Action("Subcategory", "Category", new { categoryID = parent.ID }) %>#section12">link text</a>
这篇关于在 ASP.NET MVC Html.ActionLink 中包含锚标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!