【问题标题】:Refreshing multiple partial views in ASP.NET MVC在 ASP.NET MVC 中刷新多个局部视图
【发布时间】:2020-04-18 19:33:28
【问题描述】:

提前致谢。

我正在开发类似于亚马逊上的产品过滤器视图。我刷新了多个视图,但所有局部视图的数据来自单个 ajax 调用如何刷新多个局部视图。我可以完全刷新主要内容区域,但不应该刷新某些部分视图。

【问题讨论】:

  • 我了解您是新手,但以后请粘贴一些代码来帮助您。我打了一些东西来寻求您的帮助,希望对您有所帮助:)

标签: c# ajax asp.net-mvc


【解决方案1】:

我将其分解为多个步骤,以便您可以关注/修改并添加您的部分内容,例如 here。首先,添加 3 个 Partial View,它们的代码相同,如下所示,

@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);
}

【讨论】:

  • 感谢@transformer 帮助我解决了另一个问题,当我按下浏览器后退按钮时,它会加载整个页面,但部分视图复选框和单选按钮选择不会重置。如何避免这种情况发生
  • 你好,按返回键是什么意思,你离开了整个页面,那么什么是重置!正在尝试只刷新一个partial,然后尝试this。另外,如果有效,请标记为答案!
  • 您好,感谢您的及时回复。在主页上,我呈现了一个显示单选按钮列表以过滤搜索结果的部分视图。选择其中一个单选按钮来过滤结果。然后我点击了一个导航到不同页面的按钮。现在我单击浏览器后退按钮,它会重新加载整个页面,但单选按钮选择仍然存在。这些选择应该被重置
  • function goBack() { $.ajax({ type: "GET", url: '@Url.Action("goBackPartialViewControllerMethod")', success: function(data) { // data is your view in oncern $('#divGoBackParticialView').html(data); } }); } // &lt;a href="javascript:void(0);" onclick="goBack()"&gt;Go Back View&lt;/a&gt; 如果您可以创建一个新问题,并发布您的代码和您的期望,请标记我的用户名,我会尝试帮助你。对于 cmets 部分来说,它很多。
猜你喜欢
  • 2016-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 2015-09-13
  • 2021-07-16
  • 1970-01-01
相关资源
最近更新 更多