asp.net-mvc 在 ASP.NET MVC 中围绕图像制作 Html.ActionLink?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/989005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make an Html.ActionLink around an Image in ASP.NET MVC?
提问by KingNestor
How can I do something similar to Html.ActionLink() except place the generated link around an Image instead of just spitting out the link?
除了将生成的链接放在图像周围而不是仅仅吐出链接之外,我该如何做类似于 Html.ActionLink() 的操作?
回答by Craig Stuntz
Razor (View Engine):
剃刀(查看引擎):
<a href="@Url.Action("ActionName", "ControllerName")">
<img src="@Url.Content("~/Content/img/imgname.jpg")" />
</a>
ASPX (View Engine):
ASPX(视图引擎):
<a href="<%= Url.Action("ActionName", "ControllerName") %>">
<img src="<%= Url.Content("~/Content/img/imgname.jpg") %>" />
</a>
Obviously, if you do this more than once, write a helper for it. And fill in the other attributes of img/a. But this should give you the general idea.
显然,如果您多次这样做,请为其编写一个帮助程序。并填写img/a的其他属性。但这应该给你一个大致的想法。
回答by eu-ge-ne
Try something like this:
尝试这样的事情:
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);
}
Hope this helps
希望这可以帮助
回答by Dilip0165
The first answer given by @Craig Stuntz is absolutely perfect but my concern is about if what will you do if you have Ajax.ActionLinkinstead of Html.ActionLink. Here I will explain easy solutions for both methods. You can do as the following for Html.ActonLink:
@Craig Stuntz 给出的第一个答案绝对是完美的,但我担心的是如果你有Ajax.ActionLink而不是Html.ActionLink. 在这里,我将解释这两种方法的简单解决方案。您可以执行以下操作Html.ActonLink:
@Html.Raw(@Html.ActionLink("[replacetext]", "Index", "Home").ToHtmlString().Replace("[replacetext]", "<img src=\"/Contents/img/logo.png\" ... />"))
same concept can be applied for Ajax.ActionLink
可以应用相同的概念 Ajax.ActionLink
@Html.Raw(@Ajax.ActionLink("[replacetext]", "Index", "Home", new AjaxOptions { UpdateTargetId="dvTest"}).ToHtmlString().Replace("[replacetext]", "<img src=\"/Contents/img/logo.png\" … />"))
so this will be easy for you.
所以这对你来说很容易。
Edit:
编辑:
ActionLink Image with Style Sheet or Class Name
带有样式表或类名的 ActionLink 图像
With Style sheet
带样式表
@Html.Raw(@Ajax.ActionLink("[replacetext]", "Index", "Home", new AjaxOptions { UpdateTargetId="dvTest"}).ToHtmlString().Replace("[replacetext]", "<img src=\"/assets/img/logo.png\" style=\"width:10%\" ... />"))
With Class Name
带类名
<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\" ... />"))
For more reference regarding ActionLink around Image visit ActionLink around Image in Asp.net MVC
有关 ActionLink around Image 的更多参考,请访问Asp.net MVC 中的 ActionLink around Image
回答by olga
more easy...
更容易...
change your code by:
通过以下方式更改您的代码:
<p class="site-title">@Html.ActionLink(" ", "Index", "Home",
new
{
style = "background: url('" + Url.Content("~/images/logo.png") + "') no-repeat center right; display:block; height:50px;width:50px;"
})</p>
回答by Ali.azimi
You can use url.content:
您可以使用 url.content:
@Url.Content("~/images/img/slide.png")
this return relative path
这个返回相对路径
回答by Marko
You can create htmlhelper which can return image with link... As parameters you will pass to htmlhelper values like image path and link and in htmlhelper you will use StringBuilder to format html of that linked image properly...
您可以创建 htmlhelper,它可以返回带有链接的图像...作为参数,您将传递给 htmlhelper 值,如图像路径和链接,在 htmlhelper 中,您将使用 StringBuilder 正确格式化该链接图像的 html...
cheers
干杯

