Can someone please tell me on how to load PartialView
in the same page when clicking on Html.ActionLink
. I have listed down my requirements below.
All the above steps should handle without using Ajax in MVC5. Can anyone please help me to achieve the above steps? Your help is much appreciated
Once your view is rendered the only way to load a partial view is via an Ajax call which returns the partial view to your JQuery/Javascript and then updates the DOM accordingly.
For instance:
Your HTML
<a href="#" id="loadViewButton">Load view</a>
<div id="partialViewContainer"><!-- your partial view will be loaded in here --></div>
Your JQuery:
$('#loadViewButton').on('click', function() {
$.ajax({
type: "GET"
url: '/YourController/YourAction',
success: function (data, textStatus, jqXHR) {
$('#partialViewContainer').html(data);
}
});
});
Your controller action:
[HttpGet]
public ActionResult YourAction()
{
return View("YourView");
}
When using an ActionLink, this works really well for me.
HTML
@Html.ActionLink("Load Partial Action Link", null, null, null, new { id = "loadPartialActionLink" })
<div id="AJAXContainer"></div>
jQuery
$('#loadPartialActionLink').on('click', function (e) {
e.preventDefault();
$.ajax({
type: "GET",
url: '/MyController/_MyPartial',
success: function (data, textStatus, jqXHR) {
$('#AJAXContainer').html(data);
}
});
});
Controller Action
[HttpGet]
public ActionResult _MyPartial()
{
return PartialView("_MyPartial");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With