简体   繁体   中英

ASP.NET MVC: How do I change where Ajax.BeginForm will post to after the page loads?

I want to make an Ajax.BeginForm that will by default, go to the add user page. I also want in the same form an edit and delete button. The information in the form will be the same, I just need it to post to the different edit user and delete user URL.

How can I modify where the form will post, depending on what submit button is pressed, while keeping all the items that Ajax.BeginForm gives us.

Edit:

Also wanted to note that I want the URL to be generated by my routes. So in the same way the BeingForm uses the "action" and "controler" to make the path, I want to use that as well when I change the URL, so the URL is dynamic and not static.

I was able to get this to work by using jquery to change the action to the form using Url.Actions.

The HTML button:

<button type="submit" class="btn btn-danger" id="formDeleteButton" onclick="ChangeFormToDelete()">Delete</button>

The javascript/jQuery

function ChangeFormToDelete() {
            $('#UserForm').attr('action', '@Url.Action("DeleteUser", "UserManagement")')
        }

In @Url.Action the firstparameter is the action and the second is the controller. I made the exact same functions for edit and add and it worked perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM