I'm having a little bit of trouble with refreshing a partialview on mvc4, here is the code:
<div id="partialViewDiv"></div>
<input type="button" id="firstPartialLink" value="Change Div"/>
<input type="button" id="secondPartialLink" value="Change Div"/>
<script type="text/javascript">
$(function() {
$("#firstPartialLink").click(function() {
$("#partialViewDiv").load('@Url.Action("GetDiv", "Home")');
});
$("#secondPartialLink").click(function () {
$("#partialViewDiv").load('@Url.Action("GetDiv2", "Home")');
});
})
</script>
When I press one of the buttons the first time, it renders the partialview inside the DIV, but when I press it again, nothing happens, what would be the cause?
To create a partial view, right click on the Shared folder -> click Add -> click View.. to open the Add View popup, as shown below. You can create a partial view in any View folder. However, it is recommended to create all your partial views in the Shared folder so that they can be used in multiple views.
In ASP.NET Core MVC, a controller's ViewResult is capable of returning either a view or a partial view. In Razor Pages, a PageModel can return a partial view represented as a PartialViewResult object. Referencing and rendering partial views is described in the Reference a partial view section.
To create a partial view, right-click on view -> shared folder and select Add -> View option. In this way we can add a partial view. It is not mandatory to create a partial view in a shared folder but a partial view is mostly used as a reusable component, it is a good practice to put it in the "shared" folder.
try to use this delegate to handle click event
$(document).on("click","#firstPartialLink",function() {
$("#partialViewDiv").load('/Home/GetDiv',function(html) { if need it});
});
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