Load ASP.NET MVC partial views on link click
Some one asked me how we can load partial views on link click. Here is the snippet for the same.
Long version - Using JQuery
<script type="text/javascript">
$("#loadPartialView").click(function () {
$.get('@Url.Action("LoadPartialView","Home")', {}, function (response) {
$("#Display").html(response);
});
});
</script>
And here is the small version using Ajax.ActionLink
@Ajax.ActionLink("Load Partial View", "LoadPartialView", "Home",
new AjaxOptions() { UpdateTargetId = "Display" })
In the page, you need to add a DIV with Id attribute which got a value “Display”, partial view will be loaded to this DIV.
And here is the controller action
public ActionResult LoadPartialView()
{
return PartialView("_PartialView");
}
Happy Programming :)