C# Jquery Ajax Post 后部分视图刷新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16014129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Partial View Refreshes after Jquery Ajax Post
提问by HendPro12
In my c# MVC4 application I am working with two partial views. 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. Each view contains a datatables.net datatable. 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.
在我的 c# MVC4 应用程序中,我使用了两个局部视图。局部视图 1 位于 ID 为 Partial_Analysis 的 div 中,局部视图 2 位于 ID 为 Display_Average 的 div 中。每个视图都包含一个 datatables.net 数据表。当在局部视图 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. I have tried this which causes an infinite loop.
刷新局部视图 1 后,我希望第二个局部视图也刷新。我试过这个会导致无限循环。
<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>
采纳答案by Andrei
ajaxSuccessis a global handler which is called whenever a response for an ajax call is received. Performing another ajax call in it will definitely cause an infinite loop.
ajaxSuccess是一个全局处理程序,每当收到 ajax 调用的响应时都会调用它。在其中执行另一个ajax调用肯定会导致无限循环。
Probably the best option here is to update second table in the successhandler 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);
}
}
回答by chaitanyasingu
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....
这应该对你有用......它对我有用......

