How do I render the partial view using jquery?
We can render the partial View like this:
<% Html.RenderPartial(\"UserDetails\"); %>
@tvanfosson rocks with his answer.
However, I would suggest an improvement within js and a small controller check.
When we use @Url
helper to call an action, we are going to receive a formatted html. It would be better to update the content (.html
) not the actual element (.replaceWith
).
More about at: What's the difference between jQuery's replaceWith() and html()?
$.get( '@Url.Action("details","user", new { id = Model.ID } )', function(data) {
$('#detailsDiv').html(data);
});
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:
public ActionResult Details( int id )
{
var model = GetFooModel();
if (Request.IsAjaxRequest())
{
return PartialView( "UserDetails", model );
}
return View(model);
}