2010-04-09 7 views
12

Làm cách nào để chuyển các tham số cho hàm OnSuccess của lớp AjaxOptions trong ASP.NET MVC?Làm cách nào để chuyển các tham số cho hàm OnSuccess của lớp AjaxOptions trong ASP.NET MVC?

Dưới đây là code của tôi, nhưng nó không hoạt động:

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

CẬP NHẬT

Thiết lập OnSuccess tài sản để (function(){updateCount('parameter');}) giải quyết vấn đề của tôi:

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

Trả lời

10

Bạn nên có thể sử dụng một selector jQuery để cư một giá trị từ một lĩnh vực trong trang:

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

Ngoài ra hãy xem ở đây: Can I pass a parameter with the OnSuccess event in a Ajax.ActionLink

+0

Cảm ơn bạn đã chỉ cho tôi liên kết đó. Tôi đã xem bài viết đó trước đây nhưng đã bỏ qua một trong những câu trả lời. –

+0

@Dave, làm thế nào tôi có thể vượt qua neo được tạo ra cho chức năng thành công? Tôi đã thử 'this', nhưng nó không hoạt động. – Shimmy

1

Dưới đây là một ví dụ MVC4. OnBegin, OnSuccess, OnComplete và OnFailure -functions được sử dụng để bật/tắt hoạt ảnh ajax của tôi. Mỗi hàm sẽ chuyển một Id mục làm tham số để cho phép tôi sử dụng lại các hàm js của mình cho tất cả các phần ajax của tôi. ajaxOnbegin() cho thấy một gif và ajaxOnsuccess giấu nó một lần nữa.

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

       )