Home >Web Front-end >JS Tutorial >How to Render Partial Views with jQuery in ASP.NET MVC?
Loading Partial Views with jQuery in ASP.NET MVC
Rendering partial views directly using jQuery is not feasible. However, a workaround involving a server-side call and jQuery/AJAX manipulation can achieve the same result.
Solution:
<code class="javascript">$('.js-reload-details').on('click', function(evt) { evt.preventDefault(); evt.stopPropagation();</code>
<code class="javascript"> var url = $(this).data('url');</code>
<code class="javascript"> $.get(url, function(data) { $detailDiv.replaceWith(data); });</code>
<code class="c#">public ActionResult Details( int id ) { var model = ...get user from db using id... return PartialView( "UserDetails", model ); }</code>
<code class="javascript">var $detailDiv = $('#detailsDiv'); $detailDiv.replaceWith(data);</code>
Button and Partial View Markup:
<code class="html"><!-- Parent View Button --> <button data-url='@Url.Action("details","user", new { id = Model.ID } )' class="js-reload-details">Reload</button> <!-- UserDetails partial view --> <div id="detailsDiv"> <!-- ...content... --> </div></code>
Note: This approach relies on a server-side action to render the partial view and an AJAX request to update the page using jQuery.
The above is the detailed content of How to Render Partial Views with jQuery in ASP.NET MVC?. For more information, please follow other related articles on the PHP Chinese website!