2010-04-09 11 views
12

¿Cómo puedo pasar parámetros a la función OnSuccess de la clase AjaxOptions en ASP.NET MVC?¿Cómo puedo pasar parámetros a la función OnSuccess de la clase AjaxOptions en ASP.NET MVC?

Aquí está mi código pero no funciona:

<%= Ajax.ActionLink("Delete", 
        "Delete", 
        "MyController", 
        New With {.id = record.ID}, 
        New AjaxOptions With 
        { 
         .Confirm = "Delete record?", 
         .HttpMethod = "Delete", 
         .OnSuccess = "updateCount('parameter')" 
        }) 
%> 

ACTUALIZACIÓN

Al establecer la propiedad OnSuccess-(function(){updateCount('parameter');}) resuelto mi problema:

<%= Ajax.ActionLink("Delete", 
        "Delete", 
        "MyController", 
        New With {.id = record.ID}, 
        New AjaxOptions With 
        { 
         .Confirm = "Delete record?", 
         .HttpMethod = "Delete", 
         .OnSuccess = "(function(){updateCount('parameter');})" 
        }) 
%> 

Respuesta

10

Usted debe poder usar un selector de jQuery para rellenar un valor de un campo en la página:

<%= Ajax.ActionLink("Delete", 
        "Delete", 
        "MyController", 
        New With {.id = record.ID}, 
        New AjaxOptions With 
        { 
         .Confirm = "Delete record?", 
         .HttpMethod = "Delete", 
         .OnSuccess = "updateCount($('#SomeField).val()))" 
        }) 
%> 

también echar un vistazo aquí: Can I pass a parameter with the OnSuccess event in a Ajax.ActionLink

+0

Muchas gracias por dirigirme a ese enlace. Había revisado esa publicación antes, pero pasé por alto una de las respuestas. –

+0

@Dave, ¿cómo puedo pasar el ancla generada a la función de éxito? Intenté 'esto', pero no funcionó. – Shimmy

1

Aquí está un ejemplo MVC4. Las funciones OnBegin, OnSuccess, OnComplete y OnFailure se usan para habilitar/deshabilitar mis animaciones ajax. Cada función pasa un Id de elemento como parámetro para permitirme reutilizar mis funciones js para todas mis secciones de ajax. ajaxOnbegin() muestra un gif y ajaxOnsuccess lo oculta de nuevo.

<script> 
@*Ajax Animation*@ 
    $(document).ready(function() { 
     $("#ajaxLoadingGif").hide(); 
    }); 
    function ajaxOnbegin(id) { 
     //show animated gif 
     $(id).show(); 
    } 
    function ajaxOnsuccess(id) { 
     //disable animated gif 
     $(id).hide(); 
    } 
    function ajaxOnfailure(id) { 
     //disbale animated gif 
     $(id).hide(); 
    } 
    function ajaxOncomplete(id) { 
     //disable animated gif 
     $(id).hide(); 
    } 


    </script> 

@Ajax.ActionLink(linkText: " Hi", // <-- Text to display 
        actionName: "getJobCards", // <-- Action Method Name 
        routeValues: new { searchString = ViewBag.searchString}, 
        ajaxOptions: new AjaxOptions{ 
           "#itemId", // <-- DOM element ID to update 
           InsertionMode = InsertionMode.Replace, 
           HttpMethod = "GET", // <-- HTTP method 
           OnBegin = "ajaxOnbegin('#ajaxLoadingGif')", 
              //="ajaxOnbegin" without parameters 
           OnSuccess = "ajaxOnsuccess('#ajaxLoadingGif')", 
           OnComplete = "ajaxOncomplete('#ajaxLoadingGif')", 
           OnFailure = "ajaxOnfailure('#ajaxLoadingGif')" 
           }, 
           htmlAttributes: new { id = ViewBag.ajaxId } 

       ) 
Cuestiones relacionadas