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

时间:2012-08-17 15:42:25

标签: asp.net-mvc asp.net-mvc-3 razor asp.net-ajax

我想将链接转换为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>Click Me</a>"

3 个答案:

答案 0 :(得分: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 :(得分: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;
}

我希望这段代码对你有用。

谢谢。

答案 2 :(得分: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" })