使用自定义Html Helper将图像作为链接:如何在RouteValueDictionary中传递数据

时间:2013-05-03 15:10:13

标签: c# html asp.net-mvc html-helper

我正在使用这些html助手:

    /*
     * 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
    }

我在那里找到了:

How to render an action link with an image?

它运作正常,但我承认我真的不习惯制作这些,有些东西我不明白。

例如,现在,我的图片上有一个链接,如下所示:

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

你知道,我的控制器中的“详细信息”操作使用了id,所以在这种情况下我需要链接如下:

/Object/Details/1

1表示我可以在此对象中找到的对象ID。但我不知道如何将id传递给对象,因为那里的方法使用了RouteDictionaryValue,我有不知道如何工作,所以我最终得到了这个链接:

/Object/Details

当然,它不起作用。如何将ID或所需数据传递给HtmlHelper以创建实际有效的链接?

1 个答案:

答案 0 :(得分:0)

经过多次解决后,我有点想通了!

我不得不改变我的链接:

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

@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,完美无缺。但是我仍然不明白什么是RouteValueDictionary,所以请随意发布内容,因为我一定会阅读它们!