简体   繁体   English

部分视图在Jquery Ajax Post之后刷新

[英]Partial View Refreshes after Jquery Ajax Post

In my c# MVC4 application I am working with two partial views. 在我的c#MVC4应用程序中,我正在使用两个部分视图。 Partial View 1 is located in a div with the id Partial_Analysis, Partial View 2 is in a div with the id Display_Average. 部分视图1位于具有id Partial_Analysis的div中,Partial View 2位于具有id Display_Average的div中。 Each view contains a datatables.net datatable. 每个视图都包含一个datatables.net数据表。 When a row is selected within the table in partial view one, a jquery ajax post is made that causes partial view 2 to refresh with an updated datatable showing results based off of the row selection that was made in partial view 1. 当在局部视图1中的表中选择行时,产生jquery ajax帖子,其使得部分视图2刷新,其中更新的数据表显示基于在部分视图1中进行的行选择的结果。

<script type="text/javascript" charset="utf-8">
    $(document).ready(function () {
        $('.rowselection').click(function (e) {
            var tdata = $('#form1').serialize();
            $.ajax({
                type: "POST",
                data: tdata,
                url: "Home/PartialAverage",
                success: function (result) { success(result); }
            });
        });

        function success(result) {
            $("#Display_Average").html(result);
        }
    });
</script>

When a specific button is clicked, partial view 1 is refreshed. 单击特定按钮时,将刷新部分视图1。

<script type="text/javascript" charset="utf-8">
    $(document).ready(function () {
        $('#ChangeName').click(function (e) {
            var tdata = $('#form1').serialize();
            var origname = $('#NameDiv').find('input[name="Name"]').first().val();
            var newname = $('#NameDiv').find('input[name="updatedName"]').first().val();
            $.ajax({
                type: "POST",
                data: {
                    mCollection: tdata,
                    Name: origname,
                    updatedName: newname
                },

                url: "Home/ChangeName",
                success: function (result) { success(result); }
            });
        });


        function success(result) {
            $("#Partial_Analysis").html(result);
        }
    });
</script>

Upon this refresh of partial view 1, I want the second partial view to refresh also. 在刷新局部视图1时,我希望第二部分视图也刷新。 I have tried this which causes an infinite loop. 我试过这个导致无限循环。

<script type="text/javascript" charset="utf-8">
    $(document).ready(function () {
        $('#Partial_Analysis').ajaxSuccess(function (e) {
            var tdata = $('#form1').serialize();
            $.ajax({
                type: "POST",
                data: {
                    mCollection: tdata,
                },

                url: "Home/PartialAverage",
                success: function (result) { success(result); }
            });
        });


        function success(result) {
            $("#Display_Average").html(result);
        }
    });
</script>

ajaxSuccess is a global handler which is called whenever a response for an ajax call is received. ajaxSuccess是一个全局处理程序,只要收到ajax调用的响应,就会调用它。 Performing another ajax call in it will definitely cause an infinite loop. 在其中执行另一个ajax调用肯定会导致无限循环。

Probably the best option here is to update second table in the success handler of the first partial view: 这里最好的选择可能是在第一个局部视图的success处理程序中更新第二个表:

function success(result) {
    $("#Partial_Analysis").html(result);

    reloadDisplayAverage();
}

function reloadDisplayAverage() {   
    var tdata = $('#form1').serialize();
    $.ajax({
        type: "POST",
        data: {
            mCollection: tdata,
        },
        url: "Home/PartialAverage",
        success: function (result) { success(result); }
    });

    function success(result) {
        $("#Display_Average").html(result);
    }
}

if the response is ajax response...then 如果响应是ajax响应......那么

   $.ajax({
                url: 'Home/PartialAverage',
                data: {mCollection: tdata,},
                type: 'POST',
                success: function (result) {

                    $("#Display_Average").html(data);
                }
            });

this should work for you..it did for me.... 这应该对你有用..这对我有用....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM