2010-04-09 62 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 } 

       ) 
相关问题