2012-08-17 20 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 –

+0

は、あなただけのリクエストを送信するためにjQueryを使用して考えたことはありますか? – Paul

+0

私の答えを正しいとマークすることができたら、私の答えはhttp://stackoverflow.com/questions/341649/asp-net-mvc-ajax-actionlink-with-image/17151675#17151675 – JotaBe

答えて

28

後半、すべてこれは私が使用しているものです。他の人に役立つことを願っています。私が代わりにリンクテキストの画像を表示するAjax.ActionLinkを作成している

@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

+1

正確に私が探しているもの;素晴らしい! – pookie

0

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

を適用するIこのコードがあなたに役立つことを願って。

ありがとうございます。

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