在ASP.NET MVC Html.ActionLink中包含锚标记

在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。


我怎样才能做到这一点?


呼啦一阵风
浏览 462回答 3
3回答

汪汪一只猫

有一些采用片段参数的ActionLink重载。将“ section12”作为片段传递将使您获得所要遵循的行为。例如,调用LinkExtensions.ActionLink方法(HtmlHelper,字符串,字符串,字符串,字符串,字符串,字符串,字符串,对象,对象):<%= Html.ActionLink("Link Text", "Action", "Controller", null, null, "section12-the-anchor", new { categoryid = "blah"}, null) %>
打开App,查看更多内容
随时随地看视频慕课网APP