如何为asp.net mvc4中的动作链接提供图像

时间:2013-05-19 06:59:07

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

我有一项任务是为动作链接提供图像

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

我该怎么做?

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

创建一个小扩展程序的另一种方法是在您的上下文中使用任何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?" }) 

答案 2 :(得分:0)

或使用此扩展程序并自定义:

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