如何在MVC3中的ActionLink中包装图像?

时间:2012-04-10 18:15:46

标签: asp.net-mvc-3 image

我有一个局部视图,需要在ActionLink中包装图像,所以当用户点击它时,它会去编辑图像。我有一个名为img.ashx的内容处理程序。

这是我的图片文字:  

这是我的ActionLink: @ Html.ActionLink(item.Title,“EditMediaItem”,new {location = item.Location,id = item.Id})

如何做到这一点?

提前致谢。

2 个答案:

答案 0 :(得分:13)

使用Url帮助方法Url.Action,例如:

<a href="@Url.Action(item.Title, "EditMediaItem", new { location = item.Location, id = item.Id })"><img src="#" /></a>

答案 1 :(得分:3)

我想使用此扩展程序:

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")
        {
            // 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", "/" + 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);
        }
    }

你这样使用它:

@Html.ImageLink("", "Controller", "Action", "", 
  "~/images/whatever.png", "My test link", "imagelinkcssclass")