asp.net mvc - How to render an action link with an image? -
i know use html.actionlink() render textual <a href...">
links actions.
how render link action has underlying image link?
<a href="foo"><img src="asdfasdf"/></a>
here code imagelink htmlhelper extension use.
/* * image link html helper */ /// <summary> /// return image link /// </summary> /// <param name="helper"></param> /// <param name="imageurl">url image</param> /// <param name="controller">target controller name</param> /// <param name="action">target action name</param> /// <param name="linktext">anchor text</param> public static string 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 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 string 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 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 string 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="strothers">other url parts querystring, etc</param> /// <param name="strimageurl">url image</param> /// <param name="alternatetext">alternate text image</param> /// <param name="strstyle">style of image border properties, etc</param> /// <returns></returns> public static string 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 image</param> /// <param name="alternatetext">alternate text image</param> /// <param name="strstyle">style of image border properties, etc</param> /// <param name="htmlattributes">html attribues link</param> /// <returns></returns> public static string 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 img tag tagbuilder image = new tagbuilder("img"); image.mergeattribute("src", 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 img tag inside 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 anchor.tostring(tagrendermode.normal); //to add </a> end tag }
Comments
Post a Comment