asp.net-mvc 如何使用 HTML.ActionLink 插入图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3667979/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:28:03  来源:igfitidea点击:

How do I insert an image using HTML.ActionLink?

asp.net-mvcimagehtml.actionlink

提问by r.r

how to insert image in html.actionlink - asp.net mvc?

如何在 html.actionlink - asp.net mvc 中插入图像?

i did it so, but it doesnt works.

我这样做了,但它不起作用。

<a href="<%= Html.ActionLink("search", "Search", new { searchText = "txtSearch" }, null); %>">
        <img alt="searchPage" style="vertical-align: middle;" height="17px"
            src="../../Stylesheets/search.PNG" title="search" />

回答by Trimack

You are completely misusing the ActionLinkhelper. The helper actually produces the whole <a href=".." ></a>tag.

你完全滥用了ActionLink助手。助手实际上生成整个<a href=".." ></a>标签。

What you really want to use in this case (apart from your own written helper) is this:

在这种情况下你真正想使用的(除了你自己的书面助手)是这样的:

<a href="<%= Url.Action("Search", new { searchText = "txtSearch" }) %>">
    <img alt="searchPage" style="vertical-align: middle;" height="17px"
        src="../../Stylesheets/search.PNG" title="search" />
</a>

回答by WEFX

@Trimack is 100% correct w/ MVC2. If people are searching for the MVC3 equivalent, here it is...

@Trimack 使用 MVC2 是 100% 正确的。如果人们正在寻找 MVC3 等价物,这里是......

<a href="@Url.Action("Search", new { searchText = "txtSearch" })">
    <img alt="searchPage" style="vertical-align: middle;" height="17px" src="../../Stylesheets/search.PNG" title="search" /> 
</a> 

回答by Murat Y?ld?z

If you want to display multi images you can use Html.ActionLinkproperty in your Viewas shown below. In this sample I use Detail/Edit/Delete images as a button under Action column.

如果要显示多张图像,可以使用如下所示的Html.ActionLink属性View。在这个示例中,我使用细节/编辑/删除图像作为操作列下的按钮。

Note:Type Title, Controllerand Actionname in Html.ActionLinkaccording to your project. If you want to use empty title simply type " " for them.

注:类型TitleControllerAction名称Html.ActionLink根据您的项目。如果您想使用空标题,只需为它们键入“”。

....
//for using multiple Html.ActionLink in a column using Webgrid
grid.Column("Action", format: (item) =>
 new HtmlString(
       Html.ActionLink("Show Details", "Edit", "Admin", new
       {
           applicantId = item.ApplicantID,               
           title = "Detail",
           @class = "icon-link",
           style = "background-image: url('../../Content/icons/detail.png')"
       }, null).ToString() +
       Html.ActionLink("Edit Record", "Edit", "Admin", new
       {
           applicantId = item.ApplicantID, 
           title = "Edit",
           @class = "icon-link",
           style = "background-image: url('../../Content/icons/edit.png')"
       }, null).ToString() +
       Html.ActionLink("Delete Record", "Edit", "Admin", new
       {
           applicantId = item.ApplicantID,
           title = "Delete",
           @class = "icon-link",
           style = "background-image: url('../../Content/icons/delete.png')"
       }, null).ToString()
 )
)
....


<style type="text/css">
    a.icon-link {
        background-color: transparent; 
        background-repeat: no-repeat; 
        background-position: 0px 0px;
        border: none;
        cursor: pointer; 
        width: 16px;
        height: 16px;
        margin-right: 8px; 
        vertical-align: middle;
    }
</style>

For full example, you might look at here: How to use WebGrid in a cshtml view?

有关完整示例,您可以查看此处:如何在 cshtml 视图中使用 WebGrid?

Regards.

问候。

回答by AEMLoviji

i have create a helper for this solution. So u can't include image to actionLink. But with this helper class u can simply add anchor with image to view.

我为这个解决方案创建了一个助手。所以你不能在 actionLink 中包含图像。但是有了这个助手类,你可以简单地添加带有图像的锚点来查看。

using System; using System.Text; using System.Web.Mvc; using System.Web.Mvc.Html; using System.Web.Routing; using System.Web; using System.Security.Policy;

使用系统;使用 System.Text; 使用 System.Web.Mvc; 使用 System.Web.Mvc.Html; 使用 System.Web.Routing; 使用 System.Web;使用 System.Security.Policy;

namespace Helpers
{
    public static class ActionWithImageHelper
    {
        public static string AnchorIm(this HtmlHelper helper)
        {

            var builder = new TagBuilder("img");
            var link = helper.ActionLink("[replaceInnerHTML]", "replaceAction").ToHtmlString();


                builder.MergeAttribute("src", "<imagePath>");
                builder.MergeAttribute("alt", "<altText>");

                var renderedLink = link.Replace("replaceAction", "<>");
                link = renderedLink;


            return link.Replace("[replaceInnerHTML]",builder.ToString(TagRenderMode.SelfClosing));
        }

    }
}

good luck

祝你好运

回答by Amit Ghute

Instead of using @Html.ActionLink("linkname","action","controller") you can use following:

您可以使用以下内容,而不是使用 @Html.ActionLink("linkname","action","controller"):

<a href='@Url.Action("action", "controller")'>

"images" is my folder for storing the images. @Url.Content()is to know the path. You can pass your action and the controller for that action to @Url.Action(). @Url.Action()works similar to the @Html.ActionLink(). Now your link will get replaced by the image.

“图像”是我用于存储图像的文件夹。@Url.Content()是知道路径。您可以将您的操作和该操作的控制器传递给@Url.Action()@Url.Action()工作原理类似于@Html.ActionLink(). 现在您的链接将被图像替换。