繁体   English   中英

具有HTML内容的ASP MVC ActionLinks

[英]ASP MVC ActionLinks with HTML content

有很好的扩展方法可以在ASP MVC中生成ActionLinks / RouteLinks。 但是,它仅允许您在生成的标记内编写纯文本。 如果要生成带有图像的锚点怎么办?

我想使用引导程序中的图标创建链接:

// expected result
<a href="/Customer?page=1"><i class="glyphicon glyphicon-arrow-left"></i> Previous page</a>

当您想生成简单链接时,可以使用@ Url.Action(),如下所示:

<a href="@Url.Action("Index","Customer", new{ page = 1 })"><i class="glyphicon glyphicon-arrow-left"></i> Previous page</a>

但是,当您要生成Ajax链接时,它并不是那么简单。 因为@ Ajax.ActionLink会使用由jquery-unobtrusive-ajax-min.js库处理的javascript或“ data- *”属性生成锚。

因此,我为自己的目的编写了扩展方法,以使用@ Html.BeginForm / @ Ajax.BeginForm(使用包围)的方式生成ActionLinks / RouteLinks。

用法:

// instead
@Html.ActionLink("Previous page", "Index", "Customer", new { page = 1 })

// you can write
@using(Html.BeginActionLink("Index", "Customer", new { page = 1 }) {
  <text><i class="glyphicon glyphicon-arrow-left"></i> Previous page</text>
}

// same with ajax links
@using(Ajax.BeginActionLink("Index", new { page = 1 }, new AjaxOptions { ... }) {
  <text><i class="glyphicon glyphicon-arrow-left"></i> Previous page</text>
}

方法BeginActionLink返回实现IDisposable的MvcLink类的实例。 在构造函数中,它写入开始标签,而在处置时,它写入结束标签。 大括号之间有您的代码的位置

namespace System.Web.Mvc
{
  using System.Text.RegularExpressions;

  public class MvcLink : IDisposable
  {
    internal static readonly string InnerText = "___F7ED35E0097945398D5A969F8DE2C63C___";
    private static readonly Regex RegexPattern = new Regex(@"^\s*(?<startTag>.*)\s*" + InnerText + @"\s*(?<endTag>.*)\s*$", RegexOptions.Compiled | RegexOptions.Singleline);

    private readonly ViewContext _viewContext;
    private readonly string _endTag;

    internal MvcLink(ViewContext viewContext, IHtmlString actionLink) {
      _viewContext = viewContext;
      var match = RegexPattern.Match(actionLink.ToHtmlString());
      if (match.Success) {
        var startTag = match.Groups["startTag"].Value;
        _endTag = match.Groups["endTag"].Value;
        _viewContext.Writer.Write(startTag);
      }
    }

    public void Dispose() {
      _viewContext.Writer.Write(_endTag);
    }
  }
}

然后由您来编写HtmlHelper和AjaxHelper的扩展方法。 方法ActionLink / RouteLink的重载过多,因此我只准备了我在应用程序中确实使用的重载。

但是写其他东西很容易。 您可以看到我创建了MvcLink的实例,它将ViewContext作为第一个参数,并将内置ActionLink的结果与预定义的InnerText替换为您的内容。

namespace System.Web.Mvc
{
  using System.Web.Mvc.Ajax;
  using System.Web.Mvc.Html;

  public static class MvcHelperExtensions
  {
    public static MvcLink BeginActionLink(this AjaxHelper ajaxHelper, string actionName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {
      return new MvcLink(ajaxHelper.ViewContext, ajaxHelper.ActionLink(MvcLink.InnerText, actionName, routeValues, ajaxOptions, htmlAttributes));
    }

    public static MvcLink BeginActionLink(this HtmlHelper htmlHelper, string actionName, object routeValues, object htmlAttributes) {
      return new MvcLink(htmlHelper.ViewContext, htmlHelper.ActionLink(MvcLink.InnerText, actionName, routeValues, htmlAttributes));
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM