I know how to use Html.ActionLink() to render the text link to the action.
How to render a pointer with a basic image A link as a link operation?
< /a>
This is the code for the ImageLink HtmlHelper extension I used.
/*
* Image Link HTML helper
*/
///
/// return image link
///
///
/// URL for image
/// target controller name
/// target action name
/// anchor text
public static string ImageLink(this HtmlHelper helper, string imageUrl, string controller, string action, string linkText)
{< br /> return ImageLink(helper, null, controller, action, linkText, imageUrl, null, null, nu ll, null);
}
///
/// return image link
///
/ //
/// URL for image
/// target controller name
/// target action name
/// anchor text
/// anchor attributes
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);
}
///
/// return image link
///
///
/// URL for image
/// target controller name
/// target action name
/// anchor text< /param>
/// anchor attributes
/// route values
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));
}
///
/// return image link
///
///
/// Id of link control< br /> /// target controller name
/// target action name
/// other URL parts like querystring, etc
/// URL for image
/// Alternate Text for the image
/// style of the image like border properties, etc
///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);
}
///
/// return image link
///
///
/// Id of link control
/// target controller name
/// target action name
/ // anc hor text
/// URL for image
/// Alternate Text for the image
param>
/// style of the image like border properties, etc
/// html attribues for link< /param>
///
public static string ImageLink(this HtmlHelper helper, string id, string controller, string action, string linkText, string strImageURL, string alternateText, string strStyle , IDictionaryhtmlAttributes, RouteValueDictionary routeValues)
{
// Build the 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 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.No rmal) + "" + span.ToString(TagRenderMode.Normal);
}
// Render tag
return anchor.ToString(TagRenderMode.Normal); //to add as end tag
}
I know to use Html.ActionLink() to render the text link to the action.
How to present a link to an operation that has a base image as a link?
This is the code of the ImageLink HtmlHelper extension I used.
/*
* Image Link HTML helper
*/
///
/// return image link
///
// /
/// URL for image
/// target controller name
/// target action name
/// anchor text
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);
}
///
/// return image link
///
///
/// URL for image
/// target controller name
/// target action name
/// anchor text
/// anchor attributes< br /> 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);
}
///
/// return image link
///
///
/// URL for image
/ // target controller name
/// target action name
/// anchor text
/// < param name="htmlAttributes">anchor attributes
/// route values
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));
}
///
/// return image link
///
///
/// Id of link control
/// target controller name
/// target action name
/// other URL parts like querystring, etc
/// URL for image
/// Alternate Te xt for the image
/// style of the image like border properties, etc
///
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);
}
///
/// return image link
///
///
/// Id of link control< br /> /// target controller name
/// target action name
/// anchor text
/// URL for image
/// Alternate Text for the image
/// style of the image like border properties, etc
/// html attribues for link
// /
public static string ImageLink(this HtmlHelper helper, string id, string controller, string action, string linkText, string strImageURL, string alternateText, string strStyle, IDictionaryhtmlAttributes, RouteValueDictionary routeValues)
{
// Build the 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(act ion, 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 anchor.ToString(TagRenderMode.Normal); //to add < /a> as end tag
}