除了将生成的链接放在图像周围而不是仅仅吐出链接之外,我还能做一些类似于Html.ActionLink()的事情吗?
发布于 2009-06-12 21:16:20
Razor (视图引擎):
<a href="@Url.Action("ActionName", "ControllerName")">
<img src="@Url.Content("~/Content/img/imgname.jpg")" />
</a>
ASPX (视图引擎):
<a href="<%= Url.Action("ActionName", "ControllerName") %>">
<img src="<%= Url.Content("~/Content/img/imgname.jpg") %>" />
</a>
显然,如果您多次执行此操作,请为其编写一个帮助器。并填写img/a的其他属性。但这应该会让您大致了解。
发布于 2009-06-12 21:36:52
尝试如下所示:
public static string ActionLinkWithImage(this HtmlHelper html, string imgSrc, string actionName)
{
var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
string imgUrl = urlHelper.Content(imgSrc);
TagBuilder imgTagBuilder = new TagBuilder("img");
imgTagBuilder.MergeAttribute("src", imgUrl);
string img = imgTagBuilder.ToString(TagRenderMode.SelfClosing);
string url = UrlHelper.Action(actionName);
TagBuilder tagBuilder = new TagBuilder("a") {
InnerHtml = img
};
tagBuilder.MergeAttribute("href", url);
return tagBuilder.ToString(TagRenderMode.Normal);
}
希望这能有所帮助
发布于 2014-04-23 14:30:29
@Craig Stuntz给出的第一个答案是绝对完美的,但我担心的是,如果你使用Ajax.ActionLink
而不是Html.ActionLink
,你会做什么?在这里,我将解释这两种方法的简单解决方案。您可以对Html.ActonLink
执行以下操作:
@Html.Raw(@Html.ActionLink("[replacetext]", "Index", "Home").ToHtmlString().Replace("[replacetext]", "<img src=\"/Contents/img/logo.png\" ... />"))
同样的概念也可以应用于Ajax.ActionLink
@Html.Raw(@Ajax.ActionLink("[replacetext]", "Index", "Home", new AjaxOptions { UpdateTargetId="dvTest"}).ToHtmlString().Replace("[replacetext]", "<img src=\"/Contents/img/logo.png\" … />"))
所以这对你来说很简单。
编辑:
具有样式表或类名的ActionLink图像
使用样式表
@Html.Raw(@Ajax.ActionLink("[replacetext]", "Index", "Home", new AjaxOptions { UpdateTargetId="dvTest"}).ToHtmlString().Replace("[replacetext]", "<img src=\"/assets/img/logo.png\" style=\"width:10%\" ... />"))
使用类名
<style>
.imgClass {
width:20%
}
@Html.Raw(@Ajax.ActionLink("[replacetext]", "Index", "Home", new AjaxOptions { UpdateTargetId="dvTest"}).ToHtmlString().Replace("[replacetext]", "<img src=\"/assets/img/logo.png\" class=\"imgClass\" ... />"))
有关图像周围的ActionLink的更多参考信息,请访问ActionLink around Image in Asp.net MVC
https://stackoverflow.com/questions/989005
复制相似问题