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

前端 未结 2 1720
青春惊慌失措
青春惊慌失措 2020-12-05 19:40

I am using MVC C# along with 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

相关标签:
2条回答
  • 2020-12-05 20:11

    window.location.href will 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.

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

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

    And here the DIV with ID myPartialViewContainer is the Container div which holds the content of the partial view.So we will simply reload the content of that div using jQuery load method

    $(function(){
    
       $('#activelist,#inactivelist').change(function () {
          var id="someval"; 
          var status = 'inactive';
          $("#myPartialViewContainer").load('@Url.Action("Skits","KitSection")' + '?id=' + id+ '&status=' + status)
      });
    
    });
    
    0 讨论(0)
  • 2020-12-05 20:21

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

    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.

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

    0 讨论(0)
提交回复
热议问题