c# - Custom helper loading an image and that would work as a link -


i have made custom html helper:

public static mvchtmlstring image(this htmlhelper _helper, string _url, string _alttext, object _htmlattributes) {     tagbuilder builder = new tagbuilder("image");      var path = _url.split('?');      string pathextra = "";      if (path.length > 1)     {         pathextra += "?" + path[1];     }      builder.attributes.add("src", virtualpathutility.toabsolute(path[0]) + pathextra);     builder.attributes.add("alt", _alttext);     builder.mergeattributes(new routevaluedictionary(_htmlattributes));     return mvchtmlstring.create(builder.tostring(tagrendermode.selfclosing)); } 

i works exquisitely fine. , must honest, not work, found while browsing on stackoverflow.

that being said, wondered there's way transform method both loads image , works hyperlink, clickable image?

try this.. need wrap image anchor tag. please add appropriate href value using mergeattribute method did image.

public static mvchtmlstring image(this htmlhelper _helper, string _url, string _alttext, object _htmlattributes) {     tagbuilder builder = new tagbuilder("image");     tagbuilder anchorabbuilder = new tagbuilder("a");       var path = _url.split('?');      string pathextra = "";      if (path.length > 1)     {         pathextra += "?" + path[1];     }      builder.attributes.add("src", virtualpathutility.toabsolute(path[0]) + pathextra);     builder.attributes.add("alt", _alttext);     builder.mergeattributes(new routevaluedictionary(_htmlattributes));     anchorabbuilder.innerhtml = builder.tostring(tagrendermode.selfclosing);     return mvchtmlstring.create(anchorabbuilder.tostring(tagrendermode.normal)); } 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -