使用链接文本中的html元素创建ajax actionlink

Fiv*_*ols 8 asp.net-mvc asp.net-ajax razor asp.net-mvc-3

我想将链接转换为ajax动作链接.我无法弄清楚如何在链接文本中显示html元素?

这是原始链接:

<a href="#onpageanchor" id="myId" class="myClass" title="My Title."><i class="icon"></i>Click Me</a>
Run Code Online (Sandbox Code Playgroud)

这是ajax actionlink:

@Ajax.ActionLink("<i class='icon'></i>Click Me", "MyActionMethod", new { id = "testId" },
                        new AjaxOptions
                        {
                            UpdateTargetId = "mytargetid"
                        }, new
                        {
                            id = "myId",
                            @class = "myClass",
                            title="My Title."
                        })
Run Code Online (Sandbox Code Playgroud)

呈现的链接文本是实际的字符串: "<i class='icon'></i>Click Me</a>"

Dan*_*Dan 33

晚了一年多了,但这就是我用的东西.希望它可以帮助别人.

@Ajax.RawActionLink(string.Format("<i class='icon'></i>Click Me"), "ActionResultName", null, new { item.Variable}, new AjaxOptions { HttpMethod = "Post", InsertionMode = InsertionMode.Replace, UpdateTargetId = "taget-div", LoadingElementId = "target-div" }, new { @class = "class" })
Run Code Online (Sandbox Code Playgroud)

那帮助者......

    public static MvcHtmlString RawActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes)
    {
        var repID = Guid.NewGuid().ToString();
        var lnk = ajaxHelper.ActionLink(repID, actionName, controllerName, routeValues, ajaxOptions, htmlAttributes);
        return MvcHtmlString.Create(lnk.ToString().Replace(repID, linkText));
    }  
Run Code Online (Sandbox Code Playgroud)

  • +1永远不会太晚被标记为答案 (3认同)