[英]Refreshing multiple partial views in ASP.NET MVC
提前致謝。
我正在研究類似於亞馬遜的產品過濾器視圖。 我刷新了多個視圖,但所有局部視圖的數據來自單個 ajax 調用如何刷新多個局部視圖。 我可以完全刷新主要內容區域,但不應該刷新某些部分視圖。
我把它分解成幾個步驟,這樣你就可以像這里一樣跟隨/修改和添加你的部分。 首先,添加 3 個局部視圖,它們具有相同的代碼,如下所示,
@model int
<div class="container fluid">
<h1>PartialDemo@(Model)</h1>
<h3>The views will all update when you click update button below</h3>
</div>
DashboardWidgets.cshtml,如下代碼,無論你的csthml頁面是什么
//<div class="row-fluid">
// <div class="col">
<div id="WidgetID_1" class="container">
@Html.Partial("_PartialWidget1", 1)
</div>
<div id="WidgetID_2" class="container">
@Html.Partial("_PartialWidget2", 2)
</div>
<div id="WidgetID_3" class="container">
@Html.Partial("_PartialWidget3", 3)
</div>
<div id="WidgetID_4" class="container">
@Html.Partial("_PartialWidget3", 4)
</div>
//</div> // the col
//</div> // the row
// lcik below button to update the partials above
// ***** One button will update them all like you wanted
<button type="button" onclick="UpdateMyWidgets()" class="btn btn-primary">Update All Partial View Views</button>
@section scripts{
<script type="text/javascript">
// this one button will update all your partials/widgets, you can add more partials in this function and just copy paste.
function UpdateMyWidgets() {
$.ajax({
url: "@Url.Action("Widget1")", // whom to call
type: "POST",
datatype: "HTML",
success: function (data) {
$("#WidgetID_1").html(data); // tell it which div to append on return
}
})
$.ajax({
url: "@Url.Action("Widget2")",
type: "POST",
datatype: "HTML",
success: function (data) {
$("#WidgetID_2").html(data);
}
});
$.ajax({
url: "@Url.Action("Widget3")",
type: "POST",
datatype: "HTML",
success: function (data) {
$("#WidgetID_3").html(data);
}
});
}
</script>
}
當單擊“ Update All Partial View Views
”按鈕時,它將調用“更新”方法。 如果成功,返回的數據將替換 div 的內容
后端操作 ajax 請求。
// these actions get called from the Update Buttons
public ActionResult Widget1()
{
return PartialView("_PartialWidget1", 11);
}
public ActionResult Widget2()
{
return PartialView("_PartialWidget2", 21);
}
public ActionResult Widget3()
{
return PartialView("_PartialWidget3", 31);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.