Case 1: When clicking on the Delete 1
button the Bootstrap's modal dialog pops up and when you click on the Delete
button of that popup, jquery
code below, as expected, correctly writes Test: id1
in Chrome browser's console window.
Case 2: But when you use a partial view to render the same html, the jquery
code, shown in case 2
below, does not write anything to Chrome browser's console window. I think it may have something to do with the id = DeleteBtnParentID
in the main View of Case 2
.
NOTE: Difference between the jqueries
in two cases is that in Case 2
I'm using Event Delegation while in Case 1
I did not have to.
- Case 1: Following works.
View (all by itself without partial view)
<button type="button" class="btn btn-info btn-xs" data-toggle="modal" data-target="#myModal" value="id1">Delete1</button>
<button type="button" class="btn btn-info btn-xs" data-toggle="modal" data-target="#myModal" value="id2">Delete2</button>
<button type="button" class="btn btn-info btn-xs" data-toggle="modal" data-target="#myModal" value="id3">Delete3</button>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog modal-sm">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header btn-warning" style="font-weight:bold;color:white;">
<button type="button" class="close" data-dismiss="modal">×</button>
<h5 class="modal-title modal-sm">Delete Warning</h5>
</div>
<div class="modal-body">
<p>Are you sure?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" id="DeleteBtnID" data-dismiss="modal">Delete</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
@section Scripts{
<script>
$(document).ready(function () {
$('#DeleteBtnID').on('click', function (event) {
console.log('Test: ' + $(this).attr('value'));
});
$('#myModal').on('show.bs.modal', function (e) {
var btnValue = $(e.relatedTarget).attr('value');
$('#DeleteBtnID').attr('value', btnValue);
})
});
</script>
}
- Case 2: Following dos NOT work when the above html is rendered via a Partial View:
Main View:
<div id="DeleteBtnParentID">
@Html.Partial("TestPartial")
</div>
Partial View [TestPartial.cshtml]:
<button type="button" class="btn btn-info btn-xs" data-toggle="modal" data-target="#myModal" value="id1">Delete1</button>
<button type="button" class="btn btn-info btn-xs" data-toggle="modal" data-target="#myModal" value="id2">Delete2</button>
<button type="button" class="btn btn-info btn-xs" data-toggle="modal" data-target="#myModal" value="id3">Delete3</button>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog modal-sm">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header btn-warning" style="font-weight:bold;color:white;">
<button type="button" class="close" data-dismiss="modal">×</button>
<h5 class="modal-title modal-sm">Delete Warning</h5>
</div>
<div class="modal-body">
<p>Are you sure?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" id="DeleteBtnID" data-dismiss="modal">Delete</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
@section Scripts{
<script>
$(document).ready(function () {
$('#DeleteBtnParentID').on('click', '#DeleteBtnID', function (event) {
console.log('Test: ' + $(this).attr('value'));
});
$('#myModal').on('show.bs.modal', function (e) {
var btnValue = $(e.relatedTarget).attr('value');
$('#DeleteBtnID').attr('value', btnValue);
})
});
</script>
}