如何将参数传递给ASP.NET MVC中的AjaxOptions类的OnSuccess函数?

时间:2010-04-09 14:39:28

标签: asp.net-mvc asp.net-ajax

如何将参数传递给ASP.NET MVC中OnSuccess类的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');})"
                    })
%>

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

这是一个MVC4示例。 OnBegin,OnSuccess,OnComplete和OnFailure -functions用于启用/禁用我的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 }

                  )