2012-08-17 11 views
7

Vorrei convertire un collegamento in un collegamento azione Ajax. Non riesco a capire come visualizzare gli elementi html all'interno del testo del link?creare un actionlink Ajax con elementi html nel testo del collegamento

Ecco il link originale:

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

Ecco l'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." 
         }) 

il testo del link reso è la stringa effettiva: "<i class='icon'></i>Click Me</a>"

+0

http://stackoverflow.com/questions/9194876/asp-net-mvc-ajax-actionlink-with-a-htmlstring –

+0

Avete pensato solo usando jQuery per inviare la richiesta? – Paul

+0

Per favore, vedere la mia risposta a questo in http://stackoverflow.com/questions/341649/asp-net-mvc-ajax-actionlink-with-image/17151675#17151675 – JotaBe

risposta

28

più di un anno di ritardo e tutto ma questo è quello che uso. Spero che aiuti qualcun altro.

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

Poi l'aiutante ...

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 mai troppo tardi per essere contrassegnato come risposta – Askolein

+1

Esattamente quello che sto cercando; fantastico! – pookie

0

ho creato Ajax.ActionLink per visualizzare l'immagine al posto del link testuale

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

Applicando classe

.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 spero che questo codice ti sia utile .

Grazie.

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