1

I'm trying to implement the Ajax helper as per the previous question at ASP.NET MVC Ajax.ActionLink with Image

Using Asp.net MVC 3

When I try to add the class the line return link.Replace("[rep.... gives and error for the "link.replace" code, saying "System.Web.Mvc.MvcHtmlString' does not contain a definition for 'Replace' "

Can anyone advise on this? I also get the same error if I download the referenced project

Thanks

Mark

2 Answers 2

1

You might find this helpful. It allows styling and other options:

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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but that appears to be for HtmlHelper? Is there a version to use with AjaxHelper?
0

Convert it to a regular string and back again :

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

3 Comments

Thanks, I don't quite understand you comment, can you show me the full text to replace?
Thanks Adam, I've tried that but I get an error for the MvcString part. Which assembly is that in?
woops! it was MvcHtmlString (teaches me to do from my phone haha)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.