在 ASP.NET MVC C# 中使用 Jquery 更新局部视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11122268/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:35:21  来源:igfitidea点击:

Updating partial view with Jquery in ASP.NET MVC C#

c#jqueryasp.net-mvcpartial-views

提问by Nate Pet

I am using MVC C# along with Jquery.

我正在使用 MVC C# 和 Jquery。

I have a partial view in a rather large page that has a number of tabs.

我在一个相当大的页面中有一个部分视图,它有许多选项卡。

On a click of a checkbox, I like to update the partial view WITHIN the form. What I am getting instead is just the partial view

单击复选框后,我喜欢更新表单内的部分视图。我得到的只是部分观点

Here is my code in Jquery:

这是我在 Jquery 中的代码:

   $('#activelist,#inactivelist').change(function () {

        var status = 'inactive';
        window.location.href = '@Url.Action("Skits","KitSection")' + '?id=' + id+ '&status=' + status;

    });

Any idea on how I could update the partial view within a form in terms of how I would make a call to it?

关于如何更新表单中的局部视图,以及如何调用它的任何想法?

Here is the code for the PartialView

这是 PartialView 的代码

     return PartialView(Kits);

As mentioned, what I see is just the partial view displayed and not the whole form.

如前所述,我看到的只是显示的部分视图,而不是整个表单。

采纳答案by Shyju

window.location.hrefwill reload the entire page. You need to reload some part of your page without a complete reload. We can use jQuery ajax to do this. Let's Find out what jQuery selector you can use to get the Partialview.

window.location.href将重新加载整个页面。您需要在没有完全重新加载的情况下重新加载页面的某些部分。我们可以使用 jQuery ajax 来做到这一点。让我们找出您可以使用什么 jQuery 选择器来获取 Partialview。

For example , Assume your HTML markup is like this in the main form ( view)

例如,假设您的 HTML 标记在主表单(视图)中是这样的

<div>
  <p>Some content</p>
  <div id="myPartialViewContainer">
      @Html.Partial("_FeaturedProduct")
  </div>
  <div>Some other content</div>
</div>

And here the DIVwith IDmyPartialViewContaineris the Container div which holds the content of the partial view.So we will simply reload the content of that div using jQuery loadmethod

这里的DIVwithIDmyPartialViewContainer是 Container div ,它保存了partial view. 所以我们将简单地使用 jQuery load方法重新加载那个 div 的内容

$(function(){

   $('#activelist,#inactivelist').change(function () {
      var id="someval"; 
      var status = 'inactive';
      $("#myPartialViewContainer").load('@Url.Action("Skits","KitSection")' + '?id=' + id+ '&status=' + status)
  });

});

回答by jamesmillerio

You are redirecting the user, via the window.location.href property, to the URL of your partial, hence only displaying that partial.

您通过 window.location.href 属性将用户重定向到部分的 URL,因此只显示该部分。

You should instead do an AJAX call to the partial to retrieve it's HTML and then use something like the .append method to add it to whatever container element you want it to be added to.

您应该改为对部分进行 AJAX 调用以检索它的 HTML,然后使用类似 .append 方法的方法将其添加到您希望将其添加到的任何容器元素中。

EDIT: The .load() jQuery ajax method is actually better for this specific situation.

编辑: .load() jQuery ajax 方法实际上更适合这种特定情况。