Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include anchor tag in ASP.NET MVC 5 ActionLink

Tags:

asp.net-mvc

I have searched the web for an answer and tried Brad Wilson's suggested solution here: Including an anchor tag in an ASP.NET MVC Html.ActionLink

However, this does not work for me.

Here's what I did:

In Controller/Details/_PartialView file I place an anchor tag:

<a name="anchor-point"></a>

The _PartialView file is obviously rendered when Controller/Details is rendered.

In Controller/Index I have an ActionLink as follows

<td>@Html.ActionLink("linkText", "Details", "Controller", null, null, "anchor-point", new { item => item.ID }, null)</td>

When I inspect the rendered HTML in Chrome the above link is not what I would, which is:

../Controller/Details/id#anchor-point

Have I misunderstood something or has usage changed since the original post. I am using MVC 5 ASP.NET 4.5.

Regards Craig

like image 423
Craig Roberts Avatar asked Dec 03 '14 12:12

Craig Roberts


People also ask

What is HTML ActionLink ()?

ActionLink(HtmlHelper, String, String, String, String, String, String, Object, Object) Returns an anchor element (a element) for the specified link text, action, controller, protocol, host name, URL fragment, route values, and HTML attributes.

How to write anchor Tag in ASP net?

</a> ) tag by adding new attributes. By convention, the attribute names are prefixed with asp- . The rendered anchor element's href attribute value is determined by the values of the asp- attributes.

Can an ActionLink be a post?

ActionLink is rendered as an HTML Anchor Tag (HyperLink) and hence it produces a GET request to the Controller's Action method which cannot be used to submit (post) Form in ASP.Net MVC 5 Razor.

Which is the correct way to specify an anchor hyperlink in MVC view?

The Anchor Tag Helper generates HTML anchor (<a> </a>) element by adding a new attribute. The "href" attribute of the anchor tag is created by using new attributes. The Anchor Tag Helper generates an HTML anchor (<a> </a>) element by adding new attribute.


1 Answers

That's because you're telling the browser to scroll to an element with an ID of anchor-point, yet your element you posted you have only set the name attribute. Try this:

<a id="anchor-point"></a>
like image 109
mattytommo Avatar answered Sep 29 '22 20:09

mattytommo