在ASP.NET MVC中,我试图创建一个包含 anchor 标记的链接(即,将用户定向到页面以及页面的特定部分)。

我尝试创建的URL应该如下所示:

<a href="/category/subcategory/1#section12">Title for a section on the page</a>

我的路由设置为以下标准:
routes.MapRoute("Default", "{controller}/{action}/{categoryid}");

我正在使用的 Action 链接语法是:
<%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>
<%} %>

我的 Controller 方法如下:
public ActionResult Subcategory(int categoryID)
{
   //return itemList

   return View(itemList);
}

上面的代码正确地返回了一个URL,如下所示:
<a href="/category/subcategory/1">Title for a section on the page</a>

我不知道如何添加#section12 部分。 “部分”一词只是我用来拆分页面部分的约定,而12是子类别的ID,即child.ID。

我怎样才能做到这一点?

最佳答案

我可能会手动构建链接,如下所示:

<a href="<%=Url.Action("Subcategory", "Category", new { categoryID = parent.ID }) %>#section12">link text</a>

10-04 23:08