dp.*_*dp. 150 asp.net-mvc
在ASP.NET MVC中,我正在尝试创建一个包含锚标记的链接(即,将用户指向页面,以及页面的特定部分).
我尝试创建的URL应如下所示:
<a href="/category/subcategory/1#section12">Title for a section on the page</a>
Run Code Online (Sandbox Code Playgroud)
我的路由设置符合标准:
routes.MapRoute("Default", "{controller}/{action}/{categoryid}");
Run Code Online (Sandbox Code Playgroud)
我正在使用的操作链接语法是:
<%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>
<%} %>
Run Code Online (Sandbox Code Playgroud)
我的控制器方法如下:
public ActionResult Subcategory(int categoryID)
{
//return itemList
return View(itemList);
}
Run Code Online (Sandbox Code Playgroud)
以上正确返回URL如下:
<a href="/category/subcategory/1">Title for a section on the page</a>
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何添加#section12部分."section"一词只是我用来分解页面部分的约定,而12是子类别的ID,即child.ID.
我怎样才能做到这一点?
Bra*_*son 278
ActionLink的重载采用片段参数.传递"section12"作为你的片段将获得你所追求的行为.
例如,调用LinkExtensions.ActionLink方法(HtmlHelper,String,String,String,String,String,String,Object,Object):
<%= Html.ActionLink("Link Text", "Action", "Controller", null, null, "section12-the-anchor", new { categoryid = "blah"}, null) %>
Run Code Online (Sandbox Code Playgroud)
Lor*_*zCK 97
我可能会手动构建链接,如下所示:
<a href="<%=Url.Action("Subcategory", "Category", new { categoryID = parent.ID }) %>#section12">link text</a>
Run Code Online (Sandbox Code Playgroud)
Pus*_*ots 14
我不记得在哪个版本的ASP.NET MVC(ASP.NET MVC 3+我相信)/ Razor参数标签声明或其他所谓的(参数:x)功能被引入,但对我来说这绝对是正确的方法在ASP.NET MVC中使用锚点构建链接.
@Html.ActionLink("Some link text", "MyAction", "MyController", protocol: null, hostName: null, fragment: "MyAnchor", routeValues: null, htmlAttributes: null)
Run Code Online (Sandbox Code Playgroud)
甚至连这个答案的 Ed Blackburns反模式论证也无法与之竞争.
Zap*_*ica 10
我就是这样做的:
<a href="@Url.Action("Index","Home")#features">Features</a>
Run Code Online (Sandbox Code Playgroud)