2011-02-24 51 views
5

我不知道是否有人知道,如果它可以使用任何的「開箱即用」 ASP.NET MVC3傭工生成一個「鏈接按鈕」 ......我目前使用下列內容:帶圖像的MVC3 ActionLink(但沒有MvcFutures)?

<a class="button" title="My Action" href="@Url.Action("MyAction", "MyController", new { id = item.Id })"> 
    <img alt="My Action" src="@Url.Content("~/Content/Images/MyLinkImage.png")" /> 
</a> 

我我試圖避免使用MvcFutures,但即使我能夠使用它們,我也不認爲有一種擴展方法可以實現這一點。 (我相信在這種情況下,解決辦法是推出定製幫手as seen here

最後,this post也有一個不錯的主意,通過CSS處理這個問題,但是這不是我所問...

+0

什麼是錯的您上面張貼的摘錄?它似乎沒有太多的粗俗,並且它具有以正常標準方式指定HTML屬性的優點。 (title,alt等)而且,無論如何,如果你願意的話,自己寫這樣一種擴展方法是非常容易的。 – 2011-02-24 22:11:34

+0

@Kirk Woll現在我所用的東西沒有問題,但這不是問題。我只是試圖看看是否有更好的方法 – zam6ak 2011-02-24 22:19:44

+0

自定義HTML助手解決方案有什麼問題? – 2011-02-25 07:20:06

回答

6

我使用下面的生成操作鏈接:

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 = "<a href=\"{0}\"{1}>{2}</a>"; 
      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庫,使反射的任務更容易,但如果你喜歡不採取額外的依賴,你可以替換成正反射。

圖像幫助器擴展名曾經是MvcContrib的一部分,但似乎已被刪除,很可能是因爲該功能現在內置於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<string, object> 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<string, object> 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; 
    } 
} 
+0

感謝這看起來不錯,除了似乎取決於MvcFutures(Microsoft.Web.Mvc)...雖然不錯! – zam6ak 2011-02-24 23:30:36

+0

@ zam6ak我很確定它沒有。我在同一個文件中有一堆其他擴展名,只是忘了將它從頂部刪除。 – 2011-02-25 08:09:12

+0

好酷 - 我看到導入,所以我不知道.. – zam6ak 2011-02-25 15:09:52

1

你看起來相當不錯。你應該把它包裝在一個通用的html助手中,並且每天給它打電話。我敢肯定還有其他更有趣的方面到您的應用程序比NIT採摘約UI輔助:)

+0

還有很多其他的方面......這只是一小部分是我的觀點。所以我確實需要「提取它」,但在此期間我想確保我「不要重複自己」(DRY) – zam6ak 2011-02-25 15:08:12

0

檢查在this博客文章底部與斯蒂芬瓦爾特HTML擴展方法的一個例子