【问题标题】:How to give images for the action link in asp.net mvc4如何在asp.net mvc4中为动作链接提供图像
【发布时间】:2013-05-19 06:59:06
【问题描述】:

我的任务是为操作链接提供图片

@Ajax.ActionLink("Delete", "Delete", "FiUser", new { id = item.UserID }, new AjaxOptions { HttpMethod = "POST", OnSuccess = "DeletFiUserOnSucess", Confirm = "Do you want to delete this record?" })

图片是

<img src="../../Images/delete.png" alt="delete" />

我该怎么做?

【问题讨论】:

标签: html asp.net-mvc-4 actionlink


【解决方案1】:

在Actionlink里面,定义一个MyCssClass类

@Ajax.ActionLink("Delete", "Delete", "FiUser", new { id = item.UserID }, new AjaxOptions { HttpMethod = "POST", OnSuccess = "DeletFiUserOnSucess", Confirm = "Do you want to delete this record?" }, new { @class = "MyCssClass" })

.MyCssClass
{
  background-image:url('../../Images/delete.png');
}

【讨论】:

  • 应该注意背景图像不会打印,因此如果您需要在打印时显示图像,您必须创建一个手动链接与手动图像。您可以使用 Url.Action 设置 href
【解决方案2】:

另一种方法是创建一个小扩展,你可以在上下文中使用任何 HTML 代码而不是字符串“Delete”:

public static MvcHtmlString HtmlActionLink(this AjaxHelper helper, string html, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes)
{
    var link = helper.ActionLink("[replace] ", actionName, controllerName, routeValues, ajaxOptions, htmlAttributes).ToHtmlString();
    return new MvcHtmlString(link.Replace("[replace]", html));
}

你可以这样使用:

@Ajax.HtmlActionLink("<img src='delete.png' alt=''>", "Delete", "FiUser", new { id = item.UserID }, new AjaxOptions { HttpMethod = "POST", OnSuccess = "DeletFiUserOnSucess", Confirm = "Do you want to delete this record?" }) 

【讨论】:

    【解决方案3】:

    或者使用这个扩展并自定义:

            /// <summary>
            /// Create an Ajax.ActionLink with an associated glyphicon
            /// </summary>
            /// <param name="ajaxHelper"></param>
            /// <param name="linkText"></param>
            /// <param name="actionName"></param>
            /// <param name="controllerName"></param>
            /// <param name="glyphicon"></param>
            /// <param name="ajaxOptions"></param>
            /// <param name="routeValues"></param>
            /// <param name="htmlAttributes"></param>
            /// <returns></returns>
            public static MvcHtmlString ImageActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, string glyphicon, AjaxOptions ajaxOptions, RouteValueDictionary routeValues = null, object htmlAttributes = null)
            {
                //Example of result:          
                //<a id="btnShow" href="/Customers/ShowArtworks?customerId=1" data-ajax-update="#pnlArtworks" data-ajax-success="jsSuccess"
                //data-ajax-mode="replace" data-ajax-method="POST" data-ajax-failure="jsFailure" data-ajax-confirm="confirm" data-ajax-complete="jsComplete"
                //data-ajax-begin="jsBegin" data-ajax="true">
                //  <i class="glyphicon glyphicon-pencil"></i>
                //  <span>Edit</span>
                //</a>
    
                var builderI = new TagBuilder("i");
                builderI.MergeAttribute("class", "glyphicon " + glyphicon);
                string iTag = builderI.ToString(TagRenderMode.Normal);
    
                string spanTag = "";
                if (!string.IsNullOrEmpty(linkText))
                {
                    var builderSpan = new TagBuilder("span") { InnerHtml = " " + linkText };
                    spanTag = builderSpan.ToString(TagRenderMode.Normal);
                }
    
                //Create the "a" tag that wraps
                var builderA = new TagBuilder("a");
    
                var requestContext = HttpContext.Current.Request.RequestContext;
                var uh = new UrlHelper(requestContext);
    
                builderA.MergeAttribute("href", uh.Action(actionName, controllerName, routeValues));
    
                builderA.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
                builderA.MergeAttributes((ajaxOptions).ToUnobtrusiveHtmlAttributes());
    
                builderA.InnerHtml = iTag + spanTag;
    
                return new MvcHtmlString(builderA.ToString(TagRenderMode.Normal));
            }
    

    【讨论】:

      猜你喜欢
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 2010-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-05
      相关资源
      最近更新 更多