如何制作没有文本但内部有图像的 html.actionlink

发布于 2024-09-24 00:11:04 字数 547 浏览 4 评论 0原文

这是简单的 html:

<%--
<a href="?language=de">
    <img src="/Images/Company/de.png" alt="Webseite auf Deutsch" style="border: 0;" />
</a>
--%>

我想用它们制作 html.actionlink:

 <%= Html.ActionLink("", "ChangeCulture", "Account", new { lang = "de", returnUrl = this.Request.RawUrl }, new { @style = "background-image: url(/Images/Company/de.png)", @class = "languageLink" }) %>

我想要没有文本的链接。它不起作用,第一个参数为 null 是不允许的。图像与正常情况一样短。我如何使用没有文本但有图像的 html.actionlink ?

it is simple html:

<%--
<a href="?language=de">
    <img src="/Images/Company/de.png" alt="Webseite auf Deutsch" style="border: 0;" />
</a>
--%>

i would like to make from them html.actionlink:

 <%= Html.ActionLink("", "ChangeCulture", "Account", new { lang = "de", returnUrl = this.Request.RawUrl }, new { @style = "background-image: url(/Images/Company/de.png)", @class = "languageLink" }) %>

I would like to have my link without text. it doesn't work, null in a first parameter its not allowed. image is to short as normal. how can i use html.actionlink without text but with image??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我不在是我 2024-10-01 00:11:04

这是一个解决方案。

<a href="<%= Url.RouteUrl("MyRoute", new { id = Model.Id }) %>">
   <img src="myimage.png" alt="My Image" />.
</a>

从本质上讲,ActionLink 用于文本链接,没有对应的图像链接,但您可以使用 Url.RouteUrl 来获取链接的地址,并将您喜欢的任何 HTML 代码放入锚点内(当然 W3C 允许)。

Here is a solution.

<a href="<%= Url.RouteUrl("MyRoute", new { id = Model.Id }) %>">
   <img src="myimage.png" alt="My Image" />.
</a>

Essentially, ActionLink is for text links and there isn't an equivalent for images, but you can use Url.RouteUrl to get the address for the link and put any HTML code you like inside of the anchor (W3C permitting of course).

月亮是我掰弯的 2024-10-01 00:11:04

这种方法最简单(使用 Razor):

<a href="@Url.Action("Action", "Controller", new { lang = "de", returnUrl = this.Request.RawUrl }, new { @style = "background-image: url(/Images/Company/de.png)", @class = "languageLink" })"><img src="myimage.png" alt="My Image" /></a>

This way is easiest (using Razor):

<a href="@Url.Action("Action", "Controller", new { lang = "de", returnUrl = this.Request.RawUrl }, new { @style = "background-image: url(/Images/Company/de.png)", @class = "languageLink" })"><img src="myimage.png" alt="My Image" /></a>
烟沫凡尘 2024-10-01 00:11:04

基于之前的SO问题(已关闭),这里有一个解决方案:

public static string ImageLink(this HtmlHelper htmlHelper, 
    string imgSrc, string alt, 
    string actionName, string controllerName, object routeValues, 
    object htmlAttributes, object imgHtmlAttributes)
{
    UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
    string imgtag = htmlHelper.Image(imgSrc, alt, imgHtmlAttributes);
    string url = urlHelper.Action(actionName, controllerName, routeValues);

    TagBuilder imglink = new TagBuilder("a");
    imglink.MergeAttribute("href", url);
    imglink.InnerHtml = imgtag;
    imglink.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);

    return imglink.ToString();
}

可以在此处找到原始解决方案: 是否有用于图像链接的 ASP.NET MVC HtmlHelper?

based on a previous SO question (that was closed), here's a solution:

public static string ImageLink(this HtmlHelper htmlHelper, 
    string imgSrc, string alt, 
    string actionName, string controllerName, object routeValues, 
    object htmlAttributes, object imgHtmlAttributes)
{
    UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
    string imgtag = htmlHelper.Image(imgSrc, alt, imgHtmlAttributes);
    string url = urlHelper.Action(actionName, controllerName, routeValues);

    TagBuilder imglink = new TagBuilder("a");
    imglink.MergeAttribute("href", url);
    imglink.InnerHtml = imgtag;
    imglink.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);

    return imglink.ToString();
}

the orignal can be found here: Is there an ASP.NET MVC HtmlHelper for image links?

烟柳画桥 2024-10-01 00:11:04

这是我的谦卑贡献,使用 Razor(作为示例)将 Font Awsome 字符用作图像:

<p>
@Html.ActionLink(" ", "Create", "RouteName", new { id = Model.ID }, new { @class = "fas fa-plus text-center" }) 
</p>

两个引号之间的字符是 alt+255(按 ALT 键,然后在 num 上键入 255)键盘,然后松开 ALT 键)。
事实上,“”、“”和 string.Empty 都不能与 ActionLink 一起使用,但 alt-255 可以。这是一个非常古老的技巧(来自 DOS 时代),用于显示隐藏的空白字符。到目前为止,我还没有遇到这个技巧的任何问题。

Here is my humble contribution, using Razor (as an example) for when a Font Awsome character is used as an image:

<p>
@Html.ActionLink(" ", "Create", "RouteName", new { id = Model.ID }, new { @class = "fas fa-plus text-center" }) 
</p>

The character in between the two quotes is alt+255 (hit the ALT key, then key-in 255 on the num keyboard, then release the ALT key).
Indeed, neither "" nor " " nor string.Empty work with ActionLink, but alt-255 does. This is a very old trick (from the DOS days) to display a hidden blank character. So far I have not encountered any problems with this trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文