There does not seem to be a need for a dialog box, since all you have to do is pass the user id to the controller method, which performs some actions to βbanβ the user.
You have problems with html first. You randomly generate a separate table for each user in the foreach , and also generate invalid html, duplicating the id attribute for the button. Your view also has @model Project.User , but I assume the typo and its really @model IEnumerable<Project.User> , since you cannot list a single object. Your opinion should look like
<table> <thead> ....
Please note that instead of the id button, instead of id , the id value is added instead of the id name, and the button is added as the data- attribute, so it can be found in the .click() handler.
Then add a script to access the button
var url = '@Url.Action("BansUsers", "Bans")'; $('.banuser').click(function() { var id = $(this).data('id');
And the controller method
[HttpPost] public JsonResult BansUsers(int ID) {
Please note that the ajax ( $.post() ) function will remain on the same page and allow the user to continue to βbanβ other users. Some options to consider in the success callback include (a) disabling the button, i.e. $(this).prop('disabled', true); , to prevent the button (b) from being pressed again, deleting a row from the table, i.e. $(this).closest('tr').remove(); or (c) possibly changing the button to "un-ban" the user. You can also consider displaying the confirm dialog box and only make a message if the action has been confirmed.
source share