I am trying to get JQuery events to work with partial views in ASP.NET MVC. However, after you load a partial view through Ajax, JQuery seems not able to fire events for any of the elements in the partial view. I suspect that this issue will also happen if you are using other frameworks or JavaScript libraries to load partial html code with Ajax.
For instance, consider the following example:
Controller:
public class TestController : Controller { public ActionResult Index() { return View(); } public ActionResult LoadPartialView() { return View("PartialView"); } }
Index.aspx
<script type="text/javascript"> $(document).ready(function() { $("#button").click(function() { alert('button clicked'); }); }); </script> <div> <%using(Ajax.BeginForm("LoadPartialView", new AjaxOptions { UpdateTargetId="PartialView"})){ %> Click <input type="submit" value="here" /> to load partial view. <% } %> </div> <br /><br /> <% Html.RenderPartial("PartialView"); %>
PartialView.ascx
<div id="PartialView"> Click <input type="button" id="button" value="here"></input> to display a javascript message. </div>
Once the page loads for the first time, you can click on the "Click here to display a Javascript message" and you will get a Javascript alert message that says "button clicked". However, once you click on the "Click here to load partial view", clicking on the button that is supposed to bring the Javascript alert message doesn't have any effect. It seems that the 'click' event is not being fired anymore.
Does anyone know why this issue occurs with JQuery and how to fix? This issue also occurs with other JQuery plugins that use events.
Partial helper functions will not work with jQuery Client Side scripting. The Partial View will be populated and fetched using jQuery AJAX and finally it will be rendered as HTML inside DIV using jQuery.
A partial view is a Razor markup file ( . cshtml ) without an @page directive that renders HTML output within another markup file's rendered output. The term partial view is used when developing either an MVC app, where markup files are called views, or a Razor Pages app, where markup files are called pages.
Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .
Partial View can be a parent view and we can use it as a parent view of any partial view. We can simply pass Model to show data in the partial view or we can send the data in the partial view with the help of AJAX call.
I have found a solution :
Juste try to make :
$(document).on("click", "YOUR_SELECTOR", function(e){ /// Do some stuff ... });
Instead of :
$("YOUR_SELECTOR").on("click", function(e){ /// Do some stuff ... });
The easiest way to handle this would be to utilize the live() method:
<script type="text/javascript"> $(document).ready(function() { $("#button").live('click', function() { alert('button clicked'); }); }); </script>
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