2011-05-16 74 views
1

我想實現AJAX輔助按前一個問題在ASP.NET MVC Ajax.ActionLink with Image實施ASP.NET MVC Ajax.ActionLink與圖片

使用Asp.net MVC 3

當我嘗試添加類線路返回鏈接。替換(「[代表....給出和錯誤的」link.replace「代碼,說」System.Web.Mvc.MvcHtmlString「不包含'替換'的定義」

任何人都可以提供這方面的建議嗎?如果我下載引用的項目,我也會得到相同的錯誤

感謝

馬克

回答

0

將其轉換爲普通字符串,然後再返回:

 
return new MvcHtmlString(link.ToString().Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing))); 
+0

謝謝,我不太瞭解你的評論,你能告訴我全文替換嗎? – 2011-05-16 16:29:05

+0

看到編輯上面 – 2011-05-16 17:17:43

+0

感謝亞當,我已經嘗試過,但我得到一個錯誤的MvcString部分。那是哪個組裝? – 2011-05-16 18:38:39

1

您可能會發現這是很有幫助的。它允許造型和其他選項:

public static class ImageHelpers 
{ 
    /// <summary> 
    /// return image link 
    /// </summary> 
    /// <param name="helper"></param> 
    /// <param name="id">Id of link control</param> 
    /// <param name="controller">target controller name</param> 
    /// <param name="action">target action name</param> 
    /// <param name="strOthers">other URL parts like query string, etc</param> 
    /// <param name="strImageURL">URL for image</param> 
    /// <param name="alternateText">Alternate Text for the image</param> 
    /// <param name="strStyle">style of the image like border properties, etc</param> 
    /// <returns></returns> 
    public static string ImageLink(this HtmlHelper helper, string id, string controller, string action, string strOthers, string strImageURL, 
     string alternateText, string strStyle, string cssClass = "imagelink") 
    { 
     return ImageLink(helper, id, controller, action, strOthers, strImageURL, alternateText, strStyle, null, cssClass); 
    } 
    /// <summary> 
    /// return image link 
    /// </summary> 
    /// <param name="helper"></param> 
    /// <param name="id">Id of link control</param> 
    /// <param name="controller">target controller name</param> 
    /// <param name="action">target action name</param> 
    /// <param name="strOthers">other URL parts like query string, etc</param> 
    /// <param name="strImageURL">URL for image</param> 
    /// <param name="alternateText">Alternate Text for the image</param> 
    /// <param name="strStyle">style of the image like border properties, etc</param> 
    /// <param name="htmlAttributes">html attributes for link</param> 
    /// <returns></returns> 
    public static string ImageLink(this HtmlHelper helper, string id, string controller, string action, string strOthers, string strImageURL, 
     string alternateText, string strStyle, object htmlAttributes, string cssClass = "imagelink") 
    { 
     var portalModel = ContextCache<PortalModel>.Get(ContextCache.PortalModelSessionCache); 

     // Create tag builder 
     var divBuilder = new TagBuilder("div"); 
     divBuilder.AddCssClass(cssClass); 

     var aBuilder = new TagBuilder("a"); 

     // Create valid id 
     if (!string.IsNullOrEmpty(id)) 
      aBuilder.GenerateId(id); 

     // Add attributes 
     aBuilder.MergeAttribute("href", "/" + portalModel.PortalTag + "/" + controller + "/" + action + strOthers); //form target URL 
     aBuilder.InnerHtml = "<img src='" + strImageURL + "' alt='" + alternateText + "' class='" + cssClass + "' style='border: none;'/>" + alternateText; //set the image as inner html 
     aBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes)); 

     divBuilder.InnerHtml = aBuilder.ToString(TagRenderMode.Normal); //to add </a> as end tag 

     // Render tag 
     return divBuilder.ToString(TagRenderMode.Normal); 
    } 
} 
+0

謝謝,但似乎是HtmlHelper?是否有與AjaxHelper一起使用的版本? – 2011-05-16 18:40:00