2

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

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

我怎样才能做到这一点?

4

3 回答 3

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');
}
于 2013-05-19T07:17:52.190 回答
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?" }) 
于 2013-05-19T17:22:11.837 回答
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));
        }
于 2014-11-09T20:45:44.943 回答