代码之家  ›  专栏  ›  技术社区  ›  Bryan Roth

如何将参数传递给asp.net mvc中ajaxoptions类的onSuccess函数?

  •  12
  • Bryan Roth  · 技术社区  · 15 年前

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

    这是我的代码,但不起作用:

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

    更新

    设置 成功论 属性到 (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 回复  |  直到 11 年前
        1
  •  10
  •   Community CDub    8 年前

    您应该能够使用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

        2
  •  1
  •   Griddy    11 年前

    下面是一个MVC4示例。onbegin、onsuccess、oncomplete和onfailure函数用于启用/禁用我的ajax动画。每个函数都传递一个item 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 }
    
                      )