0

我使用Html.ActionLink在单列中有一个 WebGrid 定义和三个链接。但是,当我不使用“LinkText”属性时,申请人Id 属性作为值传递给控制器​​。

另一方面,当只使用LinkTexts而不是“”时,可以成功传递id参数(下面的类型为“我的链接文本”)。但是,我不想在链接上显示文本,我只想显示图像。

我认为可能存在输入错误,或者会有其他适合 MVC4 Razor 的方法,例如@Url.Action等。这是我在 Razor View 中的代码。

请问你能帮帮我吗?

提前致谢。

看法:

//for using multiple Html.ActionLink in a column using Webgrid
grid.Column("Operations", format: (item) =>
 new HtmlString(
       Html.ActionLink("My Link Text", "Edit", "Admin", new
       {
           applicantId = item.ApplicantID,               
           title = "Detail",
           @class = "icon-link",
           style = "background-image: url('../../Content/icons/detail.png')"
       }, null).ToString() +
       Html.ActionLink(" ", "Edit", "Admin", new
       {
           applicantId = item.ApplicantID, 
           title = "Edit",
           @class = "icon-link",
           style = "background-image: url('../../Content/icons/edit.png')"
       }, null).ToString() +
       Html.ActionLink(" ", "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>
4

2 回答 2

0

您可以使用自定义 HTML Helper 方法以便轻松使用它。去做这个:

1) 创建一个名为 HtmlHelpers 的文件夹,并在此文件夹中创建一个名为 MyHelpers 的类。然后像下面这样定义你的类(你可以通过添加一些额外的属性来改进它)。

MyHelpers.cs:

using System;
using System.Web.Mvc;

namespace <YourProjectName>.WebUI.HtmlHelpers
{
    public static class MyHelpers
    {            
        public static MvcHtmlString ImageLink(this HtmlHelper html, string imagePath, string alt, string cssClass,
           string action, string controllerName)
        {
            return ActionImage(html, imagePath, alt, cssClass, action, controllerName, null);
        }

        public static MvcHtmlString ImageLink(this HtmlHelper html, string imagePath, string alt, string cssClass,
           string action, string controllerName, object routeValues)
        {
            var currentUrl = new UrlHelper(html.ViewContext.RequestContext);
            var imgTagBuilder = new TagBuilder("img"); // build the <img> tag
            imgTagBuilder.MergeAttribute("src", currentUrl.Content(imagePath));
            imgTagBuilder.MergeAttribute("title", alt);
            imgTagBuilder.MergeAttribute("class", cssClass);
            string imgHtml = imgTagBuilder.ToString(TagRenderMode.SelfClosing);
            var anchorTagBuilder = new TagBuilder("a"); // build the <a> tag
            anchorTagBuilder.MergeAttribute("href", currentUrl.Action(action, controllerName, routeValues));
            anchorTagBuilder.InnerHtml = imgHtml; // include the <img> tag inside
            string anchorHtml = anchorTagBuilder.ToString(TagRenderMode.Normal);
            return MvcHtmlString.Create(anchorHtml);
        }
    }
}

2)重建您的项目,然后将此行添加到您的 Razor 视图中:

@using <YourProjectName>.WebUI.HtmlHelpers

3)然后在您的视图中使用此 Html Helper 方法,如下所示:

@Html.ImageLink("../../Content/icons/delete.png", "Delete", "your-class", "Delete", "Admin", new { item.ApplicantID })

Smilarly,如果您想在同一列中使用多图像链接,您可以像这样合并 html 字符串:

看法:

....
grid.Column("Operations", style: "your-class", format: (item) =>
 new HtmlString(
      @Html.ActionImage("../../Content/icons/detail.png", "Detail", "your-class", "Detail", "Admin", new { item.ApplicantID }).ToString() +
      @Html.ActionImage("../../Content/icons/edit.png", "Edit", "your-class", "Edit", "Admin", new { item.ApplicantID }).ToString() +
      @Html.ActionImage("../../Content/icons/delete.png", "Delete", "your-class", "Delete", "Admin", new { item.ApplicantID }).ToString()

 )
)
...

希望这可以帮助。问候...

于 2013-11-03T20:32:03.773 回答
0

未正确调用您的操作链接。您正在将 Html 属性添加到您的路由值中。您的操作链接应如下所示:

   Html.ActionLink("My Link Text", "Detail", "Admin", new
   {
       applicantId = item.ApplicantID              
   }, new 
   {            
       title = "Detail",
       @class = "icon-link"
   })

检查此链接以查看如何隐藏链接文本,并使用 css 显示图像:CSS 隐藏文本但显示图像?

于 2013-11-05T19:12:07.613 回答