如何将参数传递给ASP中的AjaxOptions类的OnSuccess函数。净MVC吗?

时间:2023-01-13 04:07:47

How can I pass parameters to the OnSuccess function of the AjaxOptions class in ASP.NET MVC?

如何将参数传递给ASP中的AjaxOptions类的OnSuccess函数。净MVC吗?

Here's my code but it doesn't work:

这是我的代码,但不管用:

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

UPDATE

更新

Setting the OnSuccess property to (function(){updateCount('parameter');}) solved my problem:

将OnSuccess属性设置为(){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 个解决方案

#1


10  

You should be able to use a jQuery selector to populate a value from a field in the page:

您应该能够使用jQuery选择器从页面中的字段填充值:

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

Also take a look here: Can I pass a parameter with the OnSuccess event in a Ajax.ActionLink

还可以看一下这里:我可以在Ajax.ActionLink中传递一个参数与OnSuccess事件。

#2


1  

Here is a MVC4 example. OnBegin, OnSuccess, OnComplete and OnFailure -functions are used to enable/disable my ajax animations. Each function passes a item Id as parameter to allow me to reuse my js functions for all my ajax sections. ajaxOnbegin() shows a gif and ajaxOnsuccess hides it again.

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

                  )

#1


10  

You should be able to use a jQuery selector to populate a value from a field in the page:

您应该能够使用jQuery选择器从页面中的字段填充值:

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

Also take a look here: Can I pass a parameter with the OnSuccess event in a Ajax.ActionLink

还可以看一下这里:我可以在Ajax.ActionLink中传递一个参数与OnSuccess事件。

#2


1  

Here is a MVC4 example. OnBegin, OnSuccess, OnComplete and OnFailure -functions are used to enable/disable my ajax animations. Each function passes a item Id as parameter to allow me to reuse my js functions for all my ajax sections. ajaxOnbegin() shows a gif and ajaxOnsuccess hides it again.

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

                  )