I have several buttons on a page. Each button loads a different partial "view" and uses the jQuery .load() method.
It works great but I noticed that if I keep clicking on this particular button, it keeps loading the partial view over and over until I stop. I am sure I could eventually crash the site (no good!).
I tried to cache it (by decorating the controller method and duration, etc) but it didn't work. I suspect that is because I am loading this view via button clicking.
How can I prevent repeating the load of this "view" without the user even knowing?
This is my jQuery/Javascript:
//"Select Area" button - click event
$(".btnAreaDiv .btn-myClass").click(function () {
switch (this.id) {
case "btnMyButton":
$('#myDiv').load('_MyPartialView'); //to-do: only load me, if I haven't been loaded?
break;
//more code etc., more button handling
}
}
This is the contents of _MyPartialView.cshtml:
@{
Layout = null;
}
<div class="row">
<div class="col-md-6">
<div class="row" style="width:97%">
<div class="col-md-12" style="border:2px solid #c5c5c5"><span id="lblMySelector">My Selector</span></div>
</div>
</div>
<div class="col-md-6"></div>
</div>
<div class="row nopadding checkboxDiv mySelector">
<div class="col-md-12 nopadding">
<input id="myCheckBox" type="checkbox" /><label for="myCheckBox">Include all/label> <br />
</div>
</div>
And of course the ActionResult method in my controller:
public ActionResult _MyPartialView()
{
return View();
}
I have tried this, but it didn't work:
switch (this.id) {
case "btnMyButton":
if (!$('#lblMySelector').length) {
console.log('label not found', $('#lblMySelector').length);
$('#myDiv').load('_MyPartialView');
}
break;