Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery events don't work with ASP.NET MVC Partial Views

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.

like image 544
dritan Avatar asked Feb 03 '10 20:02

dritan


People also ask

Can we use jQuery in partial view?

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.

Why would you use a partial view in asp net core model view controller?

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.

Which is the way to render partial view in asp net?

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() .

What is advantage of partial view in MVC?

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.


2 Answers

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 ... }); 
like image 92
PEOudin Avatar answered Oct 12 '22 11:10

PEOudin


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>   
like image 44
mkedobbs Avatar answered Oct 12 '22 10:10

mkedobbs