开发者

ASP.net MVC 3 adding arbitrary text to the end of an ActionLink

开发者 https://www.devze.com 2023-02-11 00:27 出处:网络
Hi I have a question about Html.ActionLink. Imagine: @Html.ActionLink(item.title, \"Singlet\", new { id = item.blog_id })

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


Maybe something like this might work:

@Html.ActionLink(
    item.title, 
    "Singlet", 
    "Blog", 
    Request.Url.Scheme, 
    null, 
    "comments", 
    new { id = item.blog_id }, 
    null
)


You can do this instead:

<a href="@Url.Action("Singlet", new { id = item.blog_id })#comments">@item.title</a>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消