2

I'm attempting to delete a row using a bootstrap modal but I'm missing a key detail in my .on function. Here is all of the relevant code:

Controller.cs:

@foreach (var item in Model)
{
    <a class="btn btn-danger btn-sm" data-employee-id="@item.EmployeeID" value="@item.EmployeeID"</a>
}

<div id="modalDelete" class="modal fade" role="dialog">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title" style="text-align-last: center">Confirm Delete</h4>
        </div>
        <div class="modal-body">
            <h5 style="text-align-last: center">You are about to delete the record with an ID of <b class="title"></b>, this procedure is irreversible.</h5>
            <h5 style="text-align-last: center">Are you sure you want to delete?</h5>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-danger btn-delete">Delete</button>
            <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
        </div>
    </div>
</div>

Script.js:

$('#modalDelete').on('show.bs.modal', function (e) {
    var data = $(e.relatedTarget).data();
    $('.title', this).text(data.employeeId);
});

$('#modalDelete').on('click', '.btn-delete', function (e) {
    var id = $(this).data('employee-id');
    console.log(id);
});

Here is what is currently returned,

The model:

You are about to delete the record with an ID of 2, this procedure is irreversible.

In the console:

undefined

I cannot seem to solve this last piece. What am I missing or what do I need to add? Is this the correct way to use and modal? I have also looked into this question but was not able to resolve it.

  • Can you try the next: `$('#modalDelete .btn-delete').on('click', function (e) { var id = $('#modalDelete').data('employee-id'); console.log(id); });` – anayarojo Mar 27 '18 at 01:28
  • `this` refers to `.btn-delete`, it does not have a data attribute of `employee-id` – penleychan Mar 27 '18 at 01:33
  • I got back to this later than I wanted to...@anayarojo I don't believe this function is called because I see no log statement and I've tried logging first thing after the function is called – Qwerty_dude Mar 29 '18 at 02:11

0 Answers0