【问题标题】:Including an anchor tag in an ASP.NET MVC Html.ActionLink在 ASP.NET MVC Html.ActionLink 中包含锚标记
【发布时间】:2010-09-21 10:26:36
【问题描述】:

在 ASP.NET MVC 中,我试图创建一个包含锚标记的链接(即,将用户定向到页面和页面的特定部分)。

我尝试创建的 URL 应如下所示:

<a href="/category/subcategory/1#section12">Title for a section on the page</a>

我的路由是用标准设置的:

routes.MapRoute("Default", "{controller}/{action}/{categoryid}"); 

我使用的动作链接语法是:

<%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>
<%} %>

我的控制器方法如下:

public ActionResult Subcategory(int categoryID)
{
   //return itemList

   return View(itemList);
}

上面正确返回一个URL如下:

<a href="/category/subcategory/1">Title for a section on the page</a>

我不知道如何添加 #section12 部分。 “section”这个词只是我用来分解页面部分的约定,12是子类别的ID,即child.ID。

我该怎么做?

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    ActionLink 的重载采用 fragment 参数。将“section12”作为您的片段传递将获得您所追求的行为。

    例如调用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) %>
    

    【讨论】:

    • 这些重载是扩展库的一部分吗?我好像没听懂。
    • 有两个:public static string ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, string protocol, string hostName, string fragment, object routeValues, object htmlAttributes); public static string ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary htmlAttributes);
    • 这应该是答案。
    • Html.ActionLink 的重载允许通过传递片段来指定锚点,强制您按名称传递控制器。我不喜欢那样。如果控制器名称不正确,将出现运行时异常,而不是编译错误。
    • @RobertMcKee 如果您的链接文本不仅仅是文本,那么 Html.ActionLink() 在任何情况下都不起作用 - 您需要使用 href=@Url.Action() 样式语法。
    【解决方案2】:

    我可能会手动构建链接,如下所示:

    <a href="<%=Url.Action("Subcategory", "Category", new { categoryID = parent.ID }) %>#section12">link text</a>
    

    【讨论】:

    • 应该真正使用 @Brad Wilson 所描述的 ActionLink 的重载。
    • @mattruma 抱歉,我不同意。吻。当您可以简单地明确说明时,为什么要拥有一个充满参数的成员,其中一些保留为空。任何人都可以看到上述内容的含义,布拉德的反应令人费解,需要您深入研究智能感知。参数过多是公认的反模式..c2.com/cgi/wiki?TooManyParameters
    • 我同意。这两种方法都有效,但由于在 URL 中指定片段的方式在不久的将来不会改变,我认为这种方式实际上更具可读性和意图更清晰。如果需要,您仍然可以使用自定义方法扩展 UrlHtml 对象,其中包括添加片段字符串的简单方法。
    【解决方案3】:

    我不记得在哪个版本的 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)
    

    即使是来自 this answer 的 Ed Blackburns 反模式论点也无法与之抗衡。

    【讨论】:

    【解决方案4】:

    我只是这样做的:

    <a href="@Url.Action("Index","Home")#features">Features</a>
    

    【讨论】:

      【解决方案5】:

      这是现实生活中的例子

      @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")
      

      并且目标页面有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>
      

      【讨论】:

        【解决方案6】:

        如果您将 ActionFilter 应用于 Subcategory 操作方法,我的解决方案将起作用,只要您始终希望将用户重定向到同一个书签:

        http://spikehd.blogspot.com/2012/01/mvc3-redirect-action-to-html-bookmark.html

        它修改 HTML 缓冲区并输出一小段 javascript 来指示浏览器附加书签。

        当然,您可以修改 javascript 以手动滚动,而不是在 URL 中使用书签!

        希望对你有帮助:)

        【讨论】:

          【解决方案7】:

          我做到了,它适用于重定向到其他视图 我想如果你在它之后添加#sectionLink 会起作用

          <a class="btn yellow" href="/users/Create/@Model.Id" target="_blank">
                                                  Add As User
                                              </a>
          

          【讨论】:

            猜你喜欢
            • 2015-02-01
            • 2014-03-30
            • 1970-01-01
            • 2021-10-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-06-13
            • 1970-01-01
            相关资源
            最近更新 更多