1

I'm new to ASP.NET MVC and I have an app where on the admin page I display a list of users stored in a database, each with their own delete button.

When this delete button is clicked for a specific user, a modal pops up to ask if the admin is sure they want to delete the user. On clicking the confirm button @Url.Action("DeleteUser", "Home" , new { UserID = id }) needs to be called (where id is the selected user's id) but I don't know how to pass the user id from the delete button to the modal in order to delete the relevant user.


Delete Button: delete button html

<button type="button" class="btn btn-info btn-lg right-float-button"
        value=Model.Users[i].ID data-toggle="modal"
        data-target="#deleteUser">Delete User</button>

(the Model.Users[i].ID is an int for the current user's id)


Modal: delete user modal

<!-- Modal -->
<div class="modal fade" id="deleteUser" role="alert">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Delete User</h4>
            </div>
            <div class="modal-body">
                <p>Are you sure?</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <button onclick="location.href='@Url.Action("DeleteUser", "Home" , new { UserID = ?? })'" 
                        type="submit" class="btn btn-default">Delete</button>
            </div>
        </div>
    </div>
</div>

How can I pass Model.Users[i].ID to the modal to be used in the Url.Action where the "??" are?

triangle
  • 11
  • 5
  • 1
    Check out this. https://stackoverflow.com/questions/10626885/passing-data-to-a-bootstrap-modal – Altaf Khokhar Aug 29 '17 at 09:24
  • @IOTEnthu oh I could use two of those js functions then - one to pass the variable to the modal when the delete button is clicked and then another when the delete button in the modal is clicked. Is this bad practice? And where in the project would I put the js code? – triangle Aug 29 '17 at 21:54
  • You can pass appropriate Id by using data-id="ISBN-001122" on button click when you want to open modal. While click event of the button you can get id using this cod $(this).data('id') . Now you have respective id in your modal, you can do ajax request on event(button click within modal or else) by sending this id. – Altaf Khokhar Aug 30 '17 at 06:34

0 Answers0