包括在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).
我想创建看起来应该像下面的网址:
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>
<%} %>
我的控制器方法如下:
My controller method is as follows:
public ActionResult Subcategory(int categoryID)
{
//return itemList
return View(itemList);
}
以上正确如下返回网址:
The above correctly returns a URL as follows:
<a href="/category/subcategory/1">Title for a section on the page</a>
我无法弄清楚如何添加在#section12 的一部分。在节字就是我使用打破了页部分的约定,而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.
我怎样才能做到这一点?
How can I do this?
推荐答案
我可能会手动构建链接,像这样:
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屋!