Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 800
Nate Pet Avatar asked Jun 20 '12 14:06

Nate Pet


People also ask

Can we use jQuery in partial view?

Partial helper functions will not work with jQuery Client Side scripting. The Partial View will be populated and fetched using jQuery AJAX and finally it will be rendered as HTML inside DIV using jQuery.

Can we use multiple partial view in MVC?

You can only return one value from a function so you can't return multiple partials from one action method. If you are trying to return two models to one view, create a view model that contains both of the models that you want to send, and make your view's model the new ViewModel. E.g.

Can we use script in partial view?

A Note About Script Binding for Partial ViewsJavaScript functions can be bound to elements on the partial view; the partial view is rendered at the same time as the parent view. This happens when loading the partial view with a @Html. Action helper method, as in this section from Edit.


2 Answers

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

});
like image 105
Shyju Avatar answered Sep 18 '22 19:09

Shyju


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.

like image 39
jamesmillerio Avatar answered Sep 18 '22 19:09

jamesmillerio