Including an anchor tag in an ASP.NET MVC Html.ActionLink

dp.

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>

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?

LorenzCK

I would probably build the link manually, like this:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Include anchor tag in ASP.NET MVC 5 ActionLink

QueryString with ASP.NET MVC 6 Anchor Tag Helper

Add favicon to Html.ActionLink in ASP.NET MVC?

Using ASP.Net MVC Razor - @Html.ActionLink

Asp.net mvc Html.ActionLink not working in Bootstrap Select

ASP.NET MVC, Actionlink

ASP.NET MVC RAZOR ActionLink click

How to add actionlink in asp.net-mvc?

ASP.NET MVC: Razor ActionLink Issue

ASP.NET Core MVC ActionLink arguments

ASP.NET MVC: Html.Action / Html.ActionLink and culture specific DateTime

Anchor tag not working properly in asp.net

Is this a bug with the asp net core ActionLink Html helper?

How to add “active” class to Html.ActionLink in ASP.NET MVC

Asp.Net MVC: Html.ActionLink is adding a wierd text to my URL

How to add "active" class to Html.ActionLink in ASP.NET MVC

How to correctly use Html.ActionLink with ASP.NET MVC 4 Areas

ASP.NET MVC 5 routing - create Html.ActionLink and parse ViewContext.RouteData.Values

MVC @html.Actionlink include html i tag

Call controller with tag helpers including data from search bar in asp.net core mvc

How to create an ActionLink to a custom route in ASP.NET MVC?

use icon in ActionLink for ASP.NET MVC, Razor Page

@Ajax.ActionLink in ASP.NET 5 MVC6

ASP.NET MVC generate url with different culture parameter by ActionLink

How to send item through ActionLink to ASP.NET MVC controller?

ASP.NET MVC 4 - ListBoxFor, send selectedValue in ActionLink

MVC ActionLink html attributes

Append QueryString to href in asp.net core Anchor Helper Tag

Add dynamic text to ToolTip in Html ActionLink using ASP.NET