在ASP.NET 视是否有一个相当于Html。ActionLink帮助Img标签?

我有一个控制器的行动产出的一个动态产生JPEG,我想到使用相同的Lambda表达链接到它作为我做Href使用ActionLink.

或者,一个帮助,只是让URL的路线(再次指定用Lambda)也将是可接受的。

编辑:我最初指定的,我是采用预5,但是我看到一个测试已经被释放。因此,所有在所有的版本数字是一个不需要的信息,因为我可以升级很快:-)

有帮助吗?

解决方案

Url.Action()将为您提供Html.ActionLink大多数重载的裸URL,但我认为到目前为止,URL-from-lambda功能只能通过Html.ActionLink获得。希望他们在某些时候会为Url.Action添加类似的重载。

其他提示

您可以使用URL.Action方法

<a href="<%= Url.Action("Create")  %>"><img src="../../Content/Images/add_48.png" /></a>

这个问题比较老了,我刚刚开始使用ASP.NET MVC,当RC已经用完时,但对于那些像我这样后来发现这个问题的人来说,这可能很有趣:

至少在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>

当然,RouteUrl还有许多其他重载。

我使用了一种解决方法为ActionLink放置标记而不是文本,然后将其替换为我的图像代码。像这样:

<%= Html.ActionLink("__IMAGE_PLACEHOLDER__", "Products").Replace("__IMAGE_PLACEHOLDER__", "<img src=\"" + myImgUrl + "\" />")%>

不是最优雅的解决方案,但它确实有效。

在MVC3中,您的链接如下所示:

<a href="@Url.Action("Create")"><img src="../../Content/Images/add_48.png" /></a>

在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>

要保持图像链接无边框,您需要添加如下的CSS规则:

img
{
     border: none;
}

您可以使用此控件。它的行为类似于ActionLink。

http://agilefutures.com/index.php/2009 / 06 / actionimage-ASPNET-MVC

在MVC 2中实现起来非常简单。我已经创建了自己非常简单的扩展方法来支持Url.Action帮助器的Lambda表达式。它要求您参考MVC 2期货。
这是代码:

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);
        }
    }
}

这是您使用它的方式:

<img src="<%= Url.Action<YourController>(c => c.YourActionMethod(param1, param2)); %>" />

我知道我的帖子太晚了,但我想分享:)

我添加了类似这样的新扩展方法:

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());
    }
}

希望这会有所帮助。

Url.Content()您要找的是什么?

给它类似于Url.Content(<!> quot;〜/ path / to / something.jpg <!> quot;)它将根据应用程序根目录将其转换为适当的路径。

-Josh

我采用了上述答案并做了一些包装扩展:

    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());
        }
无论如何,

让我更容易,希望它可以帮助别人。

我尝试将 Html.Image 的输出放入 Html.ImageLink 帮助器中。

@(new HtmlString(Html.ActionLink(Html.Image("image.gif").ToString(), "myAction", "MyController").ToString().Replace("&lt;", "<").Replace("&gt;", ">")))

问题在于,ActionLink名称是编码的,所以我有<!> amp; lt而不是<!> lt;。

我刚删除了这个编码,结果对我有用。 (有没有更好的方法来代替使用替换?)

添加到其他帖子:在我的情况下(asp.net mvc 3)我想要一个图像链接作为语言选择器,所以我最终得到:

  public static MvcHtmlString ImageLink(this HtmlHelper htmlHelper, string imgSrc, string cultureName, object htmlAttributes, object imgHtmlAttributes, string languageRouteName = "lang", bool strictSelected = false)
        {
           UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
           TagBuilder imgTag = new TagBuilder("img");
           imgTag.MergeAttribute("src", imgSrc);
           imgTag.MergeAttributes((IDictionary<string, string>)imgHtmlAttributes, true);

           var language = htmlHelper.LanguageUrl(cultureName, languageRouteName, strictSelected);                      
           string url = language.Url;

           TagBuilder imglink = new TagBuilder("a");
           imglink.MergeAttribute("href", url);
           imglink.InnerHtml = imgTag.ToString();
           imglink.MergeAttributes((IDictionary<string, string>)htmlAttributes, true);

           //if the current page already contains the language parameter make sure the corresponding html element is marked
           string currentLanguage = htmlHelper.ViewContext.RouteData.GetRequiredString("lang");
           if (cultureName.Equals(currentLanguage, StringComparison.InvariantCultureIgnoreCase))
           {
              imglink.AddCssClass("selectedLanguage");
           }
           return new MvcHtmlString(imglink.ToString());
        }

内部化支持是通过语言路线完成的 - 原始来源这里

这里有很好的解决方案,但是如果你想在动作链接中拥有更多的图像呢?我就是这样做的:

     @using (Html.BeginForm("Action", "Controler", ajaxOptions))
     { 
        <button type="submit">
           <img src="image.png" />            
        </button>
     }

缺点是我仍然需要在按钮元素上做一些样式,但是你可以将所需的所有html放在那里。

它也适用于Ajax帮助程序: https://stackoverflow.com/a/19302438/961139

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top