Я разбил его на этапы, чтобы вы могли отслеживать / изменять и добавлять свои партиалы, например здесь . Сначала добавьте 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.cs html, код, подобный приведенному ниже, независимо от того, какая у вас страница 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);
}