0

I'm using these html helpers:

    /*
     * Image Link HTML helper
    */

    /// <summary>
    /// return image link
    /// </summary>
    /// <param name="_helper"></param>
    /// <param name="_imageUrl">URL for image</param>
    /// <param name="_controller">target controller name</param>
    /// <param name="_action">target action name</param>
    /// <param name="_linkText">anchor text</param>
    public static MvcHtmlString ImageLink(this HtmlHelper _helper, string _imageUrl, string _controller, 
                                   string _action, string _linkText)
    {
        return ImageLink(_helper, null, _controller, _action, _linkText, _imageUrl, null, null, null, null);
    }

    /// <summary>
    /// return image link
    /// </summary>
    /// <param name="_helper"></param>
    /// <param name="_imageUrl">URL for image</param>
    /// <param name="_controller">target controller name</param>
    /// <param name="_action">target action name</param>
    /// <param name="_linkText">anchor text</param>
    /// <param name="_htmlAttributes">anchor attributes</param>
    public static MvcHtmlString ImageLink(this HtmlHelper _helper, string _imageUrl, string _controller,
                                          string _action, string _linkText, object _htmlAttributes)
    {
        return ImageLink(_helper, null, _controller, _action, _linkText, _imageUrl, null, null, new RouteValueDictionary(_htmlAttributes), null);
    }

    /// <summary>
    /// return image link
    /// </summary>
    /// <param name="_helper"></param>
    /// <param name="_imageUrl">URL for image</param>
    /// <param name="_controller">target controller name</param>
    /// <param name="_action">target action name</param>
    /// <param name="_linkText">anchor text</param>
    /// <param name="_htmlAttributes">anchor attributes</param>
    /// <param name="_routeValues">route values</param>
    public static MvcHtmlString ImageLink(this HtmlHelper _helper, string _imageUrl, string _controller,
                                          string _action, string _linkText, object _htmlAttributes, object _routeValues)
    {
        return ImageLink(_helper, null, _controller, _action, _linkText, _imageUrl, null, null, new RouteValueDictionary(_htmlAttributes), new RouteValueDictionary(_routeValues));
    }

    /// <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="_linkText"></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 MvcHtmlString ImageLink(this HtmlHelper _helper, string _id, string _controller,
                                          string _action, string _linkText, string _strImageURL,
                                          string _alternateText, string _strStyle)
    {
        return ImageLink(_helper, _id, _controller, _action, _linkText, _strImageURL, _alternateText, _strStyle, null, null);
    }

    /// <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="_linkText">anchor text</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 attribues for link</param>
    /// <param name="_routeValues"></param>
    /// <returns></returns>
    public static MvcHtmlString ImageLink(this HtmlHelper _helper, string _id, string _controller,
                                          string _action, string _linkText, string _strImageURL, string _alternateText,
                                          string _strStyle, IDictionary<string, object> _htmlAttributes, RouteValueDictionary _routeValues)
    {
        // Build the img tag
        TagBuilder image = new TagBuilder("img");
        image.MergeAttribute("src", VirtualPathUtility.ToAbsolute(_strImageURL));
        image.MergeAttribute("alt", _alternateText);
        image.MergeAttribute("valign", "middle");
        image.MergeAttribute("border", "none");

        TagBuilder span = new TagBuilder("span");

        // Create tag builder
        var anchor = new TagBuilder("a");
        var url = new UrlHelper(_helper.ViewContext.RequestContext).Action(_action, _controller, _routeValues);

        // Create valid id
        anchor.GenerateId(_id);

        // Add attributes
        //anchor.MergeAttribute("href", "/" + controller + "/" + action); //form target URL
        anchor.MergeAttribute("href", url);
        anchor.MergeAttribute("class", "actionImage");
        if (_htmlAttributes != null)
            anchor.MergeAttributes(new RouteValueDictionary(_htmlAttributes));

        // place the img tag inside the anchor tag.
        if (String.IsNullOrEmpty(_linkText))
        {
            anchor.InnerHtml = image.ToString(TagRenderMode.Normal);
        }
        else
        {
            span.InnerHtml = _linkText;
            anchor.InnerHtml = image.ToString(TagRenderMode.Normal) + " " + span.ToString(TagRenderMode.Normal);
        }

        // Render tag
        return MvcHtmlString.Create(anchor.ToString(TagRenderMode.Normal)); //to add </a> as end tag
    }

That I found there:

How to render an action link with an image?

and it works ok, but I admit that I'm really not used to make these and there are things that I do not understand.

For example, right now, there's a link on my image, made like this:

@Html.ImageLink("objectImage", "Object", "Details", null, Model[i].m_ObjThumbnailLink, Model[i].m_ObjName, null, null, null)

You see, my "details" action in my controller works with an id, so in this case I'd need the link to read as follow:

/Object/Details/1

1 for being the object ID that I can find in this object. But I don't know how to pass the id to the object because the method up there uses a RouteDictionaryValue and I have no idea how that works, so I end up having this link:

/Object/Details

And, of course, it does not work. How can I pass the ID or needed data to the HtmlHelper to create a link that actually works?

Community
  • 1
  • 1
hsim
  • 2,000
  • 6
  • 33
  • 69

1 Answers1

0

After much workaround, I kinda figured it out!

I had to change my link:

@Html.ImageLink("objectImage", "Object", "Details", null, Model[i].m_ObjThumbnailLink, Model[i].m_ObjName, null, null, null)

like this:

@Html.ImageLink("objectImage", "Object", "Details", null, Model[i].m_ObjThumbnailLink, Model[i].m_ObjName, null, null, new RouteValueDictionary(new { @id = Model[i].m_ObjID}))

... Annd, that works perfectly. But I still don't understand what is a RouteValueDictionary, so feel free to post things around as I will assuredly read them!

hsim
  • 2,000
  • 6
  • 33
  • 69
  • 2
    You can start by reading the MSDN description [here](http://msdn.microsoft.com/en-us/library/system.web.routing.routevaluedictionary.aspx) – Iridio May 03 '13 at 15:29