gpt4 book ai didi

asp.net-mvc - 在 ASP.NET MVC Html.ActionLink 中包含 anchor 标记

转载 作者:行者123 更新时间:2023-12-03 04:22:11 24 4
gpt4 key购买 nike

在 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}"); 

我使用的操作链接语法是:

<%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 部分。 “section”一词只是我用来分解页面部分的约定,而 12 是子类别的 ID,即 child.ID。

我该怎么做?

最佳答案

ActionLink 有重载,它们采用 fragment 参数。传递“section12”作为您的片段将获得您想要的行为。

例如,调用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) %>

关于asp.net-mvc - 在 ASP.NET MVC Html.ActionLink 中包含 anchor 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/274586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com