带图像的MVC3 ActionLink(但没有MvcFutures)?

我想知道是否有人知道是否可以使用任何“开箱即用”的ASP.NET MVC3助手来生成“链接按钮”……我目前使用以下内容:

 My Action  

我试图避免使用MvcFutures,但即使我能够使用它们,我也不认为它有一个扩展方法可以实现这一点。 (我相信在这种情况下的解决方案是滚动自定义助手,如此处所示 )

最后, 这篇文章也有一个好主意通过CSS来处理这个问题,但这不是我要问的……

我使用以下内容生成操作链接:

 using System; using System.Linq.Expressions; using System.Text; using System.Web.Mvc; using System.Web.Mvc.Html; using System.Web.Routing; using Fasterflect; namespace StackOverflow.Mvc.Extensions { public static class HtmlExtensions { #region ActionImage // href image link public static string ActionImage( this HtmlHelper helper, string href, string linkText, object htmlAttributes, string alternateText, string imageSrc, object imageAttributes ) { var sb = new StringBuilder(); const string format = "{2}"; string image = helper.Image( imageSrc, alternateText, imageAttributes ).ToString(); string content = string.IsNullOrWhiteSpace( linkText ) ? image : image + linkText; sb.AppendFormat( format, href, GetAttributeString( htmlAttributes ), content ); return sb.ToString(); } // controller/action image link public static string ActionImage( this HtmlHelper helper, string controller, string action, string linkText, object htmlAttributes, string alternateText, string imageSrc, object imageAttributes ) { bool isDefaultAction = string.IsNullOrEmpty( action ) || action == "Index"; string href = "/" + (controller ?? "Home") + (isDefaultAction ? string.Empty : "/" + action); return ActionImage( helper, href, linkText, htmlAttributes, alternateText, imageSrc, imageAttributes ); } // T4MVC ActionResult image link public static string ActionImage( this HtmlHelper helper, ActionResult actionResult, string linkText, object htmlAttributes, string alternateText, string imageSrc, object imageAttributes ) { var controller = (string) actionResult.GetPropertyValue( "Controller" ); var action = (string) actionResult.GetPropertyValue( "Action" ); return ActionImage( helper, controller, action, linkText, htmlAttributes, alternateText, imageSrc, imageAttributes ); } #endregion #region Helpers private static string GetAttributeString( object htmlAttributes ) { if( htmlAttributes == null ) { return string.Empty; } const string format = " {0}=\"{1}\""; var sb = new StringBuilder(); htmlAttributes.GetType().Properties().ForEach( p => sb.AppendFormat( format, p.Name, p.Get( htmlAttributes ) ) ); return sb.ToString(); } #endregion } } 

请注意,GetAttributeString方法依赖于Fasterflect库来使reflection任务更容易,但如果您不想采用其他依赖项,则可以使用常规reflection替换它。

Image helper扩展曾经是MvcContrib的一部分,但似乎已被删除,很可能是因为该function现在内置于MVC中。 无论如何,为了完整性,我将它包含在下面:

 public static class ImageExtensions { public static MvcHtmlString Image(this HtmlHelper helper, string imageRelativeUrl, string alt, object htmlAttributes) { return Image(helper, imageRelativeUrl, alt, new RouteValueDictionary(htmlAttributes)); } public static MvcHtmlString Image(this HtmlHelper helper, string imageRelativeUrl, string alt, IDictionary htmlAttributes) { if (String.IsNullOrEmpty(imageRelativeUrl)) { throw new ArgumentException(MvcResources.Common_NullOrEmpty, "imageRelativeUrl"); } string imageUrl = UrlHelper.GenerateContentUrl(imageRelativeUrl, helper.ViewContext.HttpContext); return MvcHtmlString.Create(Image(imageUrl, alt, htmlAttributes).ToString(TagRenderMode.SelfClosing)); } public static TagBuilder Image(string imageUrl, string alt, IDictionary htmlAttributes) { if (String.IsNullOrEmpty(imageUrl)) { throw new ArgumentException(MvcResources.Common_NullOrEmpty, "imageUrl"); } TagBuilder imageTag = new TagBuilder("img"); if (!String.IsNullOrEmpty(imageUrl)) { imageTag.MergeAttribute("src", imageUrl); } if (!String.IsNullOrEmpty(alt)) { imageTag.MergeAttribute("alt", alt); } imageTag.MergeAttributes(htmlAttributes, true); if (imageTag.Attributes.ContainsKey("alt") && !imageTag.Attributes.ContainsKey("title")) { imageTag.MergeAttribute("title", (imageTag.Attributes["alt"] ?? "").ToString()); } return imageTag; } } 

你看到的片段看起来很不错。 你应该将它包装在一个通用的html帮助器中,并将其称为一天。 我相信你的应用程序还有其他更有趣的方面,而不是挑选UI助手:)

有关Stephen Walther的HTML扩展方法的示例,请查看此博客文章的底部