1

Hi I have a question about Html.ActionLink.

Imagine:

@Html.ActionLink(item.title, "Singlet", new { id = item.blog_id })

produces http://[url]/[controller]/Singlet/[id]

But what if I want to suffix some more arbitrary data at the end? For example:

http://[url]/[controller]/Singlet/[id]/#comments

To jump to the comments div. I know I can just make the string myself with something like:

@( new HtmlString(String.Format("<a href=\"Blog/Singlet/{0}/#comments\">link to comments</a>", item.blog_id)) )

But I am hoping there is a cleaner way, perhaps with ActionLink?

Thanks

Dominic
  • 62,658
  • 20
  • 139
  • 163

3 Answers3

7

Maybe something like this might work:

@Html.ActionLink(
    item.title, 
    "Singlet", 
    "Blog", 
    Request.Url.Scheme, 
    null, 
    "comments", 
    new { id = item.blog_id }, 
    null
)
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

You can do this instead:

<a href="@Url.Action("Singlet", new { id = item.blog_id })#comments">@item.title</a>
Josh M.
  • 26,437
  • 24
  • 119
  • 200
0

Have a look at this answer and see if it helps:

Create a T4MVC ActionLink with url fragment

It looks like the feature was added in ASP.NET MVC 2, so it should be available in 3 as well. Here's the documentation for the ActionLink method:

http://msdn.microsoft.com/en-us/library/dd460522.aspx

Specifically, it looks like the fragment parameter is the one you're interested in.

Community
  • 1
  • 1
Daniel T.
  • 37,212
  • 36
  • 139
  • 206