[英]How do I make an image a part of my html.ActionLink?
這可能是一個非常簡單的問題。 如何將圖像添加到Html.ActionLink
鏈接? 在常規HTML中,我會放這樣的東西:
<a href="url"><img src="whatever.jpg" />Words and Stuff too </a>
如何在生成的鏈接標記中嵌入圖像?
@Html.ActionLink("Change Database Connection","Index","Home",null,null)
我試過這樣做
@Html.ActionLink("<img src=\"../../Content/themes/base/images/web_database.png\" />Change Database Connection","Index","Home",null,null)
顯然這不起作用。它只是以純文本顯示我的img
標簽。 實現這一目標的正確方法是什么? 對於這么簡單的事情,我是否需要幫助班 ?
這是我采用和更新的兩個擴展。
public static MvcHtmlString ActionImageLink(this HtmlHelper helper, string imageUrl, string altText, string actionName, string controller, object routeValues, string _imageClass = "", object htmlAttributes = null)
{
var image = new TagBuilder("img");
image.MergeAttribute("src", imageUrl);
image.MergeAttribute("alt", altText);
if (string.IsNullOrEmpty(_imageClass) == false) image.MergeAttribute("class", _imageClass);
var link = helper.ActionLink("[replaceme]", actionName, controller, routeValues, htmlAttributes);
return new MvcHtmlString(link.ToHtmlString().Replace("[replaceme]", image.ToString(TagRenderMode.SelfClosing)));
}
public static MvcHtmlString ActionImageLink(this HtmlHelper helper, string imageUrl, string altText, RouteValueDictionary routeValues, string _imageClass = "", object htmlAttributes = null)
{
var image = new TagBuilder("img");
image.MergeAttribute("src", imageUrl);
image.MergeAttribute("alt", altText);
if (string.IsNullOrEmpty(_imageClass) == false) image.MergeAttribute("class", _imageClass);
var link = helper.ActionLink("[replaceme]", routeValues, htmlAttributes);
return new MvcHtmlString(link.ToHtmlString().Replace("[replaceme]", image.ToString(TagRenderMode.SelfClosing)));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.