简体   繁体   English

在 MVC 核心应用程序中 Ajax 成功后刷新部分视图

[英]Refreshing Partial View after Ajax success in MVC core application

I have a partial view and I want to refresh its data after updating database.我有一个部分视图,我想在更新数据库后刷新它的数据。 this is the layout:这是布局:

@{
    ViewData["Title"] = "View";
    Layout = "~/Views/Shared/xxxx.cshtml";
}
<div id="myDiv">
   <partial name="_myView.cshtml" />
</div>
<!--some other partial views-->

<div class="customerBody">
    @RenderBody()
    <!--some other partial views-->
</div>

in my partial view named _myView I use:在名为 _myView 的局部视图中,我使用:

<form asp-action="SaveAccountNo" data-ajax="true" data-ajax-success="myFunc">

and this is Jquery after form这是表格后的 Jquery

<script>
    function myFunc() {
        $("#accountChangeModal").modal('hide');
        $("#myDiv").load("/Customer?handler=MyPartial");}
   
</script>

in my Customer controller:在我的客户 controller 中:

 public PartialViewResult OnGetMyPartial()
        {
            return PartialView("_myView", /*data model*/_customer);

        }

    }

      //upadte accountNo
        [HttpPost("SaveAccountNo")]
        [ValidateAntiForgeryToken]
        public void SaveAccountNo(string NewAccountNo)
        {
            if (ModelState.IsValid)
            {
                if (NewAccountNo != null)
                {
                    //upadte accountNo
                    _customer.customer.AccountNo = NewAccountNo;

               }
            }
         }

it hides my modal window but injects whole the page in myDiv not the partial view, I put a breakpoint on OnGetMyPartial() and it didn't hit.它隐藏了我的模态 window 但在myDiv中注入整个页面而不是部分视图,我在 OnGetMyPartial() 上设置了一个断点,但它没有命中。 it seems it has never called!它似乎从未调用过! the Index action is called and because of that whole the page is injected! Index 操作被调用,因此整个页面被注入! this is my first AJAX call in MVC .net core maybe I am missing something.这是我在 MVC .net 核心中的第一个 AJAX 调用,也许我遗漏了一些东西。

You said you are using MVC, but the format of the load path is that in Razor Page.您说您使用的是MVC,但是加载路径的格式是Razor页面中的格式。 Maybe you can replace it with below:也许您可以将其替换为以下内容:

$("#myDiv").load("/Customer/MyPartial");

and change your action name:并更改您的操作名称:

 public PartialViewResult MyPartial()
 {
     return PartialView("_myView", /*data model*/_customer);
 }

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

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