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

Nate Pet picture Nate Pet · Jun 20, 2012 · Viewed 48.5k times · Source

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 partial view WITHIN the form. What I am getting instead is just the partial view

Here is my code in 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

     return PartialView(Kits);

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

Answer

Shyju picture Shyju · Jun 20, 2012

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

});