Asp.net-Mvc

在連結文本中使用 html 元素創建 ajax actionlink

  • September 6, 2016

我想將連結轉換為 ajax 操作連結。我不知道如何在連結文本中顯示 html 元素?

這是原始連結:

<a href="#onpageanchor" id="myId" class="myClass" title="My Title."><i class="icon"></i>Click Me</a>

這是ajax操作連結:

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

晚了一年多,但這是我用的。希望它可以幫助別人。

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

引用自:https://stackoverflow.com/questions/12008899