Home > Article > Web Front-end > How to Render a Partial View Using jQuery in ASP.NET MVC?
In ASP.NET MVC, partial views are commonly rendered using the Html.RenderPartial() method. However, there may be scenarios where you need to render the partial view using jQuery instead.
To render a partial view using jQuery, you can do the following:
Example:
<code class="javascript">$('.js-reload-details').on('click', function(evt) { evt.preventDefault(); evt.stopPropagation(); var $detailDiv = $('#detailsDiv'), url = $(this).data('url'); $.get(url, function(data) { $detailDiv.replaceWith(data); }); });</code>
In this example, a button with the class js-reload-details triggers the click event handler. The event handler loads the partial view using $.get() and replaces the contents of the div with id detailsDiv with the returned HTML.
<code class="csharp">public ActionResult Details( int id ) { var model = ...get user from db using id... return PartialView( "UserDetails", model ); }</code>
<code class="html"><button data-url='@Url.Action("details","user", new { id = Model.ID } )' class="js-reload-details">Reload</button></code>
<code class="html"><div id="detailsDiv"> <!-- ...content... --> </div></code>
The above is the detailed content of How to Render a Partial View Using jQuery in ASP.NET MVC?. For more information, please follow other related articles on the PHP Chinese website!