2012-08-17 12 views
7

Me gustaría convertir un enlace a un enlace de acción ajax. No puedo entender cómo mostrar elementos html dentro del texto del enlace?crear ajax actionlink con elementos html en el texto del enlace

Aquí es el enlace original:

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

Aquí es el 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." 
         }) 

el texto del enlace prestado es la cadena real: "<i class='icon'></i>Click Me</a>"

+0

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

+0

¿Ha pensado simplemente usando jQuery para enviar la solicitud? – Paul

+0

Por favor, vea mi respuesta a esto en http://stackoverflow.com/questions/341649/asp-net-mvc-ajax-actionlink-with-image/17151675#17151675 – JotaBe

Respuesta

28

más de un año de retraso y todo pero esto es lo que uso. Espero que ayude a alguien más.

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

Entonces el ayudante ...

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 nunca es demasiado tarde para ser marcado como la respuesta – Askolein

+1

Exactamente lo que estoy buscando; ¡fantástico! – pookie

0

He creado Ajax.ActionLink para mostrar la imagen en lugar de enlace de texto

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

Aplicando clase

.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 Espero que este código te sea útil .

Gracias.

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