How do I render the partial view using jquery?
We can render the partial View like this:
<% Html.RenderPartial("UserDetails"); %>
How can we do the same using jquery?
How do I render the partial view using jquery? We can render the partial View like this:
How can we do the same using jquery? |
|||||
|
You can't render a partial view using only jQuery. You can, however, call a method (action) that will render the partial view for you and add it to the page using jQuery/AJAX. In the below, we have a button click handler that loads the url for the action from a data attribute on the button and fires off a GET request to replace the DIV contained in the partial view with the updated contents.
where the user controller has an action named details that does:
This is assuming that your partial view is a container with the id Parent View Button
UserDetails partial view
|
|||||||||||||||||||||
|
I have used ajax load to do this:
|
|||||||||||||||||||||
|
@tvanfosson rocks with his answer. However, I would suggest an improvement within js and a small controller check. When we use More about at: What's the difference between jQuery's replaceWith() and html()?
This is specially useful in trees, where the content can be changed several times. At the controller we can reuse the action depending on requester:
|
||||
|
You'll need to create an Action on your Controller that returns the rendered result of the "UserDetails" partial view or control. Then just use an Http Get or Post from jQuery to call the Action to get the rendered html to be displayed. |
|||||
|
Another thing you can try (based on tvanfosson's answer) is this:
And then in the scripts section of your page:
This renders your @Html.RenderAction using ajax. And to make it all fansy sjmansy you can add a fade-in effect using this css:
Man I love mvc :-) |
|||||||||
|