在ASP.NET MVC中,我尝试创建一个包含锚标记的链接(即,将用户定向到页面和页面的特定部分)。
我尝试创建的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>
<%} %>
我的控制器方法如下:
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。
我该怎么做呢?
发布于 2008-11-08 11:31:00
我可能会手动构建链接,如下所示:
<a href="<%=Url.Action("Subcategory", "Category", new { categoryID = parent.ID }) %>#section12">link text</a>
发布于 2008-11-09 01:53:44
存在采用片段参数的ActionLink重载。将"section12“作为您的片段传递会得到您想要的行为。
<%= Html.ActionLink("Link Text", "Action", "Controller", null, null, "section12-the-anchor", new { categoryid = "blah"}, null) %>
发布于 2014-09-29 02:52:33
我不记得在哪个版本的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)
即使是来自this answer的Ed Blackburns反模式论点也无法与之竞争。
https://stackoverflow.com/questions/274586
复制相似问题