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?
This question is related to
asp.net-mvc
I Did that and it works for redirecting to other view I think If you add the #sectionLink after It will work
<a class="btn yellow" href="/users/Create/@Model.Id" target="_blank">
Add As User
</a>
My solution will work if you apply the ActionFilter to the Subcategory action method, as long as you always want to redirect the user to the same bookmark:
http://spikehd.blogspot.com/2012/01/mvc3-redirect-action-to-html-bookmark.html
It modifies the HTML buffer and outputs a small piece of javascript to instruct the browser to append the bookmark.
You could modify the javascript to manually scroll, instead of using a bookmark in the URL, of course!
Hope it helps :)
There are overloads of ActionLink which take a fragment parameter. Passing "section12" as your fragment will get you the behavior you're after.
For example, calling LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, String, String, String, Object, Object):
<%= Html.ActionLink("Link Text", "Action", "Controller", null, null, "section12-the-anchor", new { categoryid = "blah"}, null) %>
I just did it like this:
<a href="@Url.Action("Index","Home")#features">Features</a>
I don't remember in which version of ASP.NET MVC (ASP.NET MVC 3+ I believe) / Razor the parameterlabeldeclaration or whatever it's called (parameter: x) feature was introduced, but to me this is definitely the proper way to build a link with an anchor in ASP.NET MVC.
@Html.ActionLink("Some link text", "MyAction", "MyController", protocol: null, hostName: null, fragment: "MyAnchor", routeValues: null, htmlAttributes: null)
Not even Ed Blackburns antipattern argument from this answer can compete with that.
There are overloads of ActionLink which take a fragment parameter. Passing "section12" as your fragment will get you the behavior you're after.
For example, calling LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, String, String, String, Object, Object):
<%= Html.ActionLink("Link Text", "Action", "Controller", null, null, "section12-the-anchor", new { categoryid = "blah"}, null) %>
There are overloads of ActionLink which take a fragment parameter. Passing "section12" as your fragment will get you the behavior you're after.
For example, calling LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, String, String, String, Object, Object):
<%= Html.ActionLink("Link Text", "Action", "Controller", null, null, "section12-the-anchor", new { categoryid = "blah"}, null) %>
My solution will work if you apply the ActionFilter to the Subcategory action method, as long as you always want to redirect the user to the same bookmark:
http://spikehd.blogspot.com/2012/01/mvc3-redirect-action-to-html-bookmark.html
It modifies the HTML buffer and outputs a small piece of javascript to instruct the browser to append the bookmark.
You could modify the javascript to manually scroll, instead of using a bookmark in the URL, of course!
Hope it helps :)
There are overloads of ActionLink which take a fragment parameter. Passing "section12" as your fragment will get you the behavior you're after.
For example, calling LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, String, String, String, Object, Object):
<%= Html.ActionLink("Link Text", "Action", "Controller", null, null, "section12-the-anchor", new { categoryid = "blah"}, null) %>
Here is the real life example
@Html.Grid(Model).Columns(columns =>
{
columns.Add()
.Encoded(false)
.Sanitized(false)
.SetWidth(10)
.Titled(string.Empty)
.RenderValueAs(x => @Html.ActionLink("Edit", "UserDetails", "Membership", null, null, "discount", new { @id = @x.Id }, new { @target = "_blank" }));
}).WithPaging(200).EmptyText("There Are No Items To Display")
And the target page has TABS
<ul id="myTab" class="nav nav-tabs" role="tablist">
<li class="active"><a href="#discount" role="tab" data-toggle="tab">Discount</a></li>
</ul>
I just did it like this:
<a href="@Url.Action("Index","Home")#features">Features</a>
I don't remember in which version of ASP.NET MVC (ASP.NET MVC 3+ I believe) / Razor the parameterlabeldeclaration or whatever it's called (parameter: x) feature was introduced, but to me this is definitely the proper way to build a link with an anchor in ASP.NET MVC.
@Html.ActionLink("Some link text", "MyAction", "MyController", protocol: null, hostName: null, fragment: "MyAnchor", routeValues: null, htmlAttributes: null)
Not even Ed Blackburns antipattern argument from this answer can compete with that.
I Did that and it works for redirecting to other view I think If you add the #sectionLink after It will work
<a class="btn yellow" href="/users/Create/@Model.Id" target="_blank">
Add As User
</a>
Here is the real life example
@Html.Grid(Model).Columns(columns =>
{
columns.Add()
.Encoded(false)
.Sanitized(false)
.SetWidth(10)
.Titled(string.Empty)
.RenderValueAs(x => @Html.ActionLink("Edit", "UserDetails", "Membership", null, null, "discount", new { @id = @x.Id }, new { @target = "_blank" }));
}).WithPaging(200).EmptyText("There Are No Items To Display")
And the target page has TABS
<ul id="myTab" class="nav nav-tabs" role="tablist">
<li class="active"><a href="#discount" role="tab" data-toggle="tab">Discount</a></li>
</ul>
Source: Stackoverflow.com