asp.net-mvc 相当于 ASP.NET MVC 中 ActionLink 的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/210711/
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
Image equivalent of ActionLink in ASP.NET MVC
提问by Jason Whitehorn
In ASP.NET MVC is there an equivalent of the Html.ActionLink helper for Img tags?
在 ASP.NET MVC 中是否有等效于 Img 标签的 Html.ActionLink 助手?
I have a controller action that outputs a dynamically generated JPEG and I wanted to use the same Lambda expressions to link to it as I do HREFs using ActionLink.
我有一个输出动态生成的 JPEG 的控制器动作,我想使用相同的 Lambda 表达式来链接到它,就像我使用 ActionLink 做 HREF 一样。
Alternatively, a helper that just gives the URL to a route (again specified using Lambdas) would also be acceptable.
或者,只为路由提供 URL(再次使用 Lambdas 指定)的助手也是可以接受的。
EDIT: I had originally specified that I was using Preview 5, however I see that a Beta has been released. So all-in-all the version number was an unneeded piece of info as I may be upgrading soon :-)
编辑:我最初指定我使用的是预览版 5,但是我看到测试版已经发布。所以总而言之,版本号是一个不需要的信息,因为我可能很快就会升级:-)
采纳答案by stevemegson
Url.Action() will get you the bare URL for most overloads of Html.ActionLink, but I think that the URL-from-lambda functionality is only available through Html.ActionLink so far. Hopefully they'll add a similar overload to Url.Action at some point.
Url.Action() 将为您提供大多数 Html.ActionLink 重载的裸 URL,但我认为 URL-from-lambda 功能目前只能通过 Html.ActionLink 使用。希望他们会在某个时候为 Url.Action 添加类似的重载。
回答by Robert MacLean
You can use the URL.Action method
您可以使用 URL.Action 方法
<a href="<%= Url.Action("Create") %>"><img src="../../Content/Images/add_48.png" /></a>
回答by realMarkusSchmidt
This question is older, and I just started recently with ASP.NET MVC when the RC was already out, but for those who find this question later like me this might be interesting:
这个问题比较老,我最近刚开始用 ASP.NET MVC 当 RC 已经出来的时候,但对于那些像我这样后来发现这个问题的人来说,这可能很有趣:
At least in the RC you can use Url.Action() also with anonymous types, the result looks much nicer than the suggestions above, I guess:
至少在 RC 中,您还可以将 Url.Action() 与匿名类型一起使用,结果看起来比上面的建议要好得多,我猜:
<a href="<%= Url.RouteUrl("MyRoute", new { param1 = "bla", param2 = 5 }) %>">
put in <span>whatever</span> you want, also <img src="a.gif" alt="images" />.
</a>
There are many other overloads for RouteUrl as well, of course.
当然,RouteUrl 也有许多其他重载。
回答by Alan Mendelevich
I used a workaround to place a marker instead of text for ActionLink and then replace it with my image code. Something like this:
我使用了一种解决方法为 ActionLink 放置一个标记而不是文本,然后用我的图像代码替换它。像这样的东西:
<%= Html.ActionLink("__IMAGE_PLACEHOLDER__", "Products").Replace("__IMAGE_PLACEHOLDER__", "<img src=\"" + myImgUrl + "\" />")%>
Not the most elegant solution but it works.
不是最优雅的解决方案,但它有效。
回答by WEFX
In MVC3, your link would look like this:
在 MVC3 中,您的链接如下所示:
<a href="@Url.Action("Create")"><img src="../../Content/Images/add_48.png" /></a>
回答by adamjcooper
In ASP.NET MVC Beta, you can use the Html.BuildUrlFromExpressionmethod in the Futures assembly (which is not included in the default ASP.NET MVC install, but is available from CodePlex) to create a link around an image--or any HTML--using the lambda-style ActionLink syntax, like this:
在 ASP.NET MVC Beta 中,您可以使用Futures 程序集中的 Html.BuildUrlFromExpression方法(该方法不包含在默认的 ASP.NET MVC 安装中,但可从CodePlex 获得)围绕图像创建链接 - 或任何HTML——使用 lambda 风格的 ActionLink 语法,如下所示:
<a href="<%=Html.BuildUrlFromExpression<MyController>(c => c.MyAction())%>">
<%=Html.Image("~/Content/MyImage.gif")%>
</a>
To keep your image links borderless, you'll need to add a CSS rule like this:
为了让你的图片链接无边框,你需要添加一个这样的 CSS 规则:
img
{
border: none;
}
回答by adamjcooper
You can use this control.It behaves like ActionLink.
您可以使用此控件。它的行为类似于 ActionLink。
http://agilefutures.com/index.php/2009/06/actionimage-aspnet-mvc
http://agilefutures.com/index.php/2009/06/actionimage-aspnet-mvc
回答by Wahid Bitar
I know that my post is too late but i wanna share :)
我知道我的帖子为时已晚,但我想分享 :)
I added new extension method something like this :
我添加了这样的新扩展方法:
public static class ImageExtensions
{
public static MvcHtmlString ImageLink(this HtmlHelper htmlHelper, string imgSrc, string additionalText = null, string actionName = null, string controllerName = null, object routeValues = null, object linkHtmlAttributes = null, object imgHtmlAttributes = null)
{
var urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
var url = "#";
if (!string.IsNullOrEmpty(actionName))
url = urlHelper.Action(actionName, controllerName, routeValues);
var imglink = new TagBuilder("a");
imglink.MergeAttribute("href", url);
imglink.InnerHtml = htmlHelper.Image(imgSrc, imgHtmlAttributes) + " " + additionalText;
linkHtmlAttributes = new RouteValueDictionary(linkHtmlAttributes);
imglink.MergeAttributes((IDictionary<string, object>)linkHtmlAttributes, true);
return MvcHtmlString.Create(imglink.ToString());
}
public static MvcHtmlString Image(this HtmlHelper htmlHelper, string imgSrc, object imgHtmlAttributes = null)
{
var imgTag = new TagBuilder("img");
imgTag.MergeAttribute("src", imgSrc);
if (imgHtmlAttributes != null)
{
imgHtmlAttributes = new RouteValueDictionary(imgHtmlAttributes);
imgTag.MergeAttributes((IDictionary<string, object>)imgHtmlAttributes, true);
}
return MvcHtmlString.Create(imgTag.ToString());
}
}
Hope this helped.
希望这有帮助。
回答by Maksymilian Majer
It's pretty simple to achieve in MVC 2. I have created my own very simple extension method to support Lambda expressions for the Url.Action helper. It requires that you reference MVC 2 Futures.
Here's the code:
在 MVC 2 中实现非常简单。我创建了自己的非常简单的扩展方法来支持 Url.Action 助手的 Lambda 表达式。它要求您引用 MVC 2 Futures。
这是代码:
using System;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Routing;
using ExpressionHelperInternal=Microsoft.Web.Mvc.Internal.ExpressionHelper;
namespace Bnv.Bssi.Web.Infrastructure.Helpers
{
public static class UrlExtensions
{
public static string Action<TController>(this UrlHelper helper, Expression<Action<TController>> action) where TController : Controller
{
RouteValueDictionary routeValuesFromExpression = ExpressionHelperInternal.GetRouteValuesFromExpression<TController>(action);
return helper.Action(routeValuesFromExpression["action"].ToString(), routeValuesFromExpression);
}
}
}
This is how you use it:
这是你如何使用它:
<img src="<%= Url.Action<YourController>(c => c.YourActionMethod(param1, param2)); %>" />
回答by Jon
I took the above answers and made a bit of a wrapper extension:
我接受了上述答案并做了一些包装扩展:
public static MvcHtmlString ActionImageLink(this HtmlHelper helper, string src, string altText, UrlHelper url, string actionName, string controllerName)
{
return ActionImageLink(helper, src, altText, url, actionName, controllerName, null, null);
}
public static MvcHtmlString ActionImageLink(this HtmlHelper helper, string src, string altText, UrlHelper url, string actionName, string controllerName, Dictionary<string, string> linkAttributes, Dictionary<string, string> imageAttributes)
{
return ActionImageLink(helper, src, altText, url, actionName, controllerName, null, linkAttributes, imageAttributes);
}
public static MvcHtmlString ActionImageLink(this HtmlHelper helper, string src, string altText, UrlHelper url, string actionName, string controllerName, dynamic routeValues, Dictionary<string, string> linkAttributes, Dictionary<string, string> imageAttributes)
{
var linkBuilder = new TagBuilder("a");
linkBuilder.MergeAttribute("href", routeValues == null ? url.Action(actionName, controllerName) : url.Action(actionName, controllerName, routeValues));
var imageBuilder = new TagBuilder("img");
imageBuilder.MergeAttribute("src", url.Content(src));
imageBuilder.MergeAttribute("alt", altText);
if (linkAttributes != null)
{
foreach (KeyValuePair<string, string> attribute in linkAttributes)
{
if (!string.IsNullOrWhiteSpace(attribute.Key) && !string.IsNullOrWhiteSpace(attribute.Value))
{
linkBuilder.MergeAttribute(attribute.Key, attribute.Value);
}
}
}
if (imageAttributes != null)
{
foreach (KeyValuePair<string, string> attribute in imageAttributes)
{
if (!string.IsNullOrWhiteSpace(attribute.Key) && !string.IsNullOrWhiteSpace(attribute.Value))
{
imageBuilder.MergeAttribute(attribute.Key, attribute.Value);
}
}
}
linkBuilder.InnerHtml = MvcHtmlString.Create(imageBuilder.ToString(TagRenderMode.SelfClosing)).ToString();
return MvcHtmlString.Create(linkBuilder.ToString());
}
has made it easier for me anyway, hope it helps someone else.
无论如何让我更容易,希望它可以帮助别人。

