Asp.net-Mvc

如何將參數傳遞給 ASP.NET MVC 中 AjaxOptions 類的 OnSuccess 函式?

  • January 29, 2014

如何將參數傳遞給ASP.NET MVCOnSuccess中的類函式?AjaxOptions

這是我的程式碼,但它不起作用:

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

更新

設置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');})"
                   })
%>

您應該能夠使用 jQuery 選擇器從頁面中的欄位填充值:

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

也看看這裡:我可以在 Ajax.ActionLink 中通過 OnSuccess 事件傳遞參數嗎

這是一個 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 }

                 )

引用自:https://stackoverflow.com/questions/2608317