如何使用 HTML.ActionLink 插入图像?

发布于 2024-09-18 09:48:19 字数 358 浏览 6 评论 0原文

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

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

<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" />

how to insert image in 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" />

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

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

发布评论

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

评论(5

ぇ气 2024-09-25 09:48:19

您完全滥用了 ActionLink 帮助器。帮助器实际上生成整个 标记。

在这种情况下,您真正​​想要使用的(除了您自己编写的助手之外)是:

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

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

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>
多像笑话 2024-09-25 09:48:19

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

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

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

我已经为此解决方案创建了一个助手。所以你不能将图像包含到actionLink中。但是通过这个辅助类,您可以简单地添加带有图像的锚点以进行查看。

使用系统;
使用系统文本;
使用 System.Web.Mvc;
使用 System.Web.Mvc.Html;
使用 System.Web.Routing;
使用系统.Web;
使用系统.安全.策略;

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

    }
}

祝你好运

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.

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;

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

复古式 2024-09-25 09:48:19

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

注意:Html.ActionLink中输入TitleControllerAction名称,根据你的项目。如果您想使用空标题,只需输入“”即可。

....
//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>

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

问候。

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

Note: Type Title, Controller and Action name in Html.ActionLink according to your project. If you want to use empty title simply type " " for them.

....
//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?

Regards.

岁月染过的梦 2024-09-25 09:48:19

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

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

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

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

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

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