2012-08-17 48 views
7

我想將鏈接轉換爲ajax操作鏈接。我無法弄清楚如何在鏈接文本中顯示html元素?在鏈接文本中使用html元素創建ajax actionlink

以下是原文鏈接:

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

+0

http://stackoverflow.com/questions/9194876/asp-net-mvc-ajax-actionlink-with-a-htmlstring – 2012-08-17 15:47:27

+0

你有沒有想過只是使用jQuery發送請求? – Paul 2012-08-17 16:09:39

+0

請參閱我對此的回答http://stackoverflow.com/questions/341649/asp-net-mvc-ajax-actionlink-with-image/17151675#17151675 – JotaBe 2013-06-17 17:19:19

回答

28

在今年年底和一切但這是我使用的。希望它可以幫助別人。

@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)); 
    } 
+2

+1永遠不會太遲被標記爲答案 – Askolein 2014-07-25 08:50:05

+1

正是我在找什麼;太棒了! – pookie 2016-07-25 17:10:02

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

我形象希望這段代碼對你有用。

謝謝。

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