使用HTML元素创建带有Ajax的ActionLink

9
我可以把链接转换成ajax动作链接。但我无法弄清如何在链接文本中显示HTML元素?
以下是原始链接:
<a href="#onpageanchor" id="myId" class="myClass" title="My Title."><i class="icon"></i>Click Me</a>

这里是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."
                        })

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

https://dev59.com/lGDVa4cB1Zd3GeqPazHN - Forte L.
你有没有考虑只使用jQuery来发送请求? - Paul
请查看我在https://dev59.com/ZXRC5IYBdhLWcg3wUfJ2#17151675中的回答。 - JotaBe
@FiveTools,如果我的回答确实帮到了你,你能否将其标记为正确的答案? - Dan
3个回答

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" })

然后帮助者把瓶子拿给了那个人,那个人就喝了一口水,然后又喝了第二口、第三口。他们两个人坐在那里,直到太阳落山。

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

1
正是我所寻找的,太棒了! - pookie

0
@Ajax.ActionLink(" Name", "AjaxGetAllUsers", "Admin", new { sortBy = ViewBag.SortByName, search = Request.QueryString["search"] }, new AjaxOptions() { UpdateTargetId = "userlist", InsertionMode = InsertionMode.Replace, HttpMethod = "GET" }, new { @class = "fa fa-sort" })

0
我已经创建了Ajax.ActionLink来显示图像而不是链接文本。
    @Ajax.ActionLink(".", "Delete_Share_Permission", "Share", new { delwhich = "one", delid = @UserSharingDetails.PK_User_Sharing_Id }, new AjaxOptions { UpdateTargetId = "sharelist", InsertionMode = InsertionMode.Replace, OnBegin = "return confirmdeleteone();" }, new { @class = "deleteButton", @id = "fna" })

应用类

 .deleteButton {        
    background-image: url("/images/DeleteData.png");
    position: absolute;
    right: 6px;
    background-repeat: no-repeat;
    border: none;
    background-position: 50% 50%;
    margin-left: 10px;
    background-color: transparent;
    width: 13px;
    color: #ffffff !important;
    display: block;
    top: 5px;
}

希望这段代码对你有用。

谢谢。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接