2010-04-09 123 views
12

如何傳遞參數,在ASP.NET MVC的AjaxOptions類的OnSuccess功能?如何將參數傳遞給ASP.NET MVC中的AjaxOptions類的OnSuccess函數?

這裏是我的代碼,但它不工作:

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

UPDATE

OnSuccess屬性設置爲(function(){updateCount('parameter');})解決我的問題:

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

回答

10

你應該能夠使用jQuery選擇從場中的頁面填入值:

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

這裏也看看:Can I pass a parameter with the OnSuccess event in a Ajax.ActionLink

+0

非常感謝我指向那個鏈接。我之前看過那篇文章,但忽略了其中的一個答案。 – 2010-04-09 14:52:45

+0

@Dave,我如何將生成的錨點傳遞給成功函數?我試過'這個',但它沒有奏效。 – Shimmy 2013-05-09 01:16:21

1

這裏是一個MVC4例子。 OnBegin,OnSuccess,OnComplete和OnFailure - 函數用於啓用/禁用我的ajax動畫。每個函數都傳遞一個項目ID作爲參數,以允許我爲所有的Ajax部分重用我的js函數。 ajaxOnbegin()顯示一個gif,並且ajaxOnsuccess再次隱藏它。

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

       ) 
相關問題